Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
21f8ae8f
Commit
21f8ae8f
authored
Jun 29, 2010
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
strconv: fix %.2g, 40
Fixes #845. R=rsc CC=golang-dev
https://golang.org/cl/1673049
parent
1246ad83
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
4 deletions
+21
-4
src/pkg/strconv/ftoa.go
src/pkg/strconv/ftoa.go
+10
-4
src/pkg/strconv/ftoa_test.go
src/pkg/strconv/ftoa_test.go
+11
-0
No files found.
src/pkg/strconv/ftoa.go
View file @
21f8ae8f
...
@@ -154,21 +154,27 @@ func genericFtoa(bits uint64, fmt byte, prec int, flt *floatInfo) string {
...
@@ -154,21 +154,27 @@ func genericFtoa(bits uint64, fmt byte, prec int, flt *floatInfo) string {
case
'f'
:
case
'f'
:
return
fmtF
(
neg
,
d
,
prec
)
return
fmtF
(
neg
,
d
,
prec
)
case
'g'
,
'G'
:
case
'g'
,
'G'
:
// trailing zeros are removed.
// trailing fractional zeros in 'e' form will be trimmed.
if
prec
>
d
.
nd
{
eprec
:=
prec
prec
=
d
.
nd
if
eprec
>
d
.
nd
&&
d
.
nd
>=
d
.
dp
{
eprec
=
d
.
nd
}
}
// %e is used if the exponent from the conversion
// %e is used if the exponent from the conversion
// is less than -4 or greater than or equal to the precision.
// is less than -4 or greater than or equal to the precision.
// if precision was the shortest possible, use precision 6 for this decision.
// if precision was the shortest possible, use precision 6 for this decision.
eprec
:=
prec
if
shortest
{
if
shortest
{
eprec
=
6
eprec
=
6
}
}
exp
:=
d
.
dp
-
1
exp
:=
d
.
dp
-
1
if
exp
<
-
4
||
exp
>=
eprec
{
if
exp
<
-
4
||
exp
>=
eprec
{
if
prec
>
d
.
nd
{
prec
=
d
.
nd
}
return
fmtE
(
neg
,
d
,
prec
-
1
,
fmt
+
'e'
-
'g'
)
return
fmtE
(
neg
,
d
,
prec
-
1
,
fmt
+
'e'
-
'g'
)
}
}
if
prec
>
d
.
dp
{
prec
=
d
.
nd
}
return
fmtF
(
neg
,
d
,
max
(
prec
-
d
.
dp
,
0
))
return
fmtF
(
neg
,
d
,
max
(
prec
-
d
.
dp
,
0
))
}
}
...
...
src/pkg/strconv/ftoa_test.go
View file @
21f8ae8f
...
@@ -34,6 +34,17 @@ var ftoatests = []ftoaTest{
...
@@ -34,6 +34,17 @@ var ftoatests = []ftoaTest{
ftoaTest
{
200000
,
'g'
,
-
1
,
"200000"
},
ftoaTest
{
200000
,
'g'
,
-
1
,
"200000"
},
ftoaTest
{
2000000
,
'g'
,
-
1
,
"2e+06"
},
ftoaTest
{
2000000
,
'g'
,
-
1
,
"2e+06"
},
// g conversion and zero suppression
ftoaTest
{
400
,
'g'
,
2
,
"4e+02"
},
ftoaTest
{
40
,
'g'
,
2
,
"40"
},
ftoaTest
{
4
,
'g'
,
2
,
"4"
},
ftoaTest
{
.4
,
'g'
,
2
,
"0.4"
},
ftoaTest
{
.04
,
'g'
,
2
,
"0.04"
},
ftoaTest
{
.004
,
'g'
,
2
,
"0.004"
},
ftoaTest
{
.0004
,
'g'
,
2
,
"0.0004"
},
ftoaTest
{
.00004
,
'g'
,
2
,
"4e-05"
},
ftoaTest
{
.000004
,
'g'
,
2
,
"4e-06"
},
ftoaTest
{
0
,
'e'
,
5
,
"0.00000e+00"
},
ftoaTest
{
0
,
'e'
,
5
,
"0.00000e+00"
},
ftoaTest
{
0
,
'f'
,
5
,
"0.00000"
},
ftoaTest
{
0
,
'f'
,
5
,
"0.00000"
},
ftoaTest
{
0
,
'g'
,
5
,
"0"
},
ftoaTest
{
0
,
'g'
,
5
,
"0"
},
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment