Skip to content

Commit 04baaf3

Browse files
authored
Correct format gG precision handling (#91)
1 parent 0cab747 commit 04baaf3

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/fmtcore.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,14 @@ function _pfmt_e(out::IO, fs::FormatSpec, x::AbstractFloat)
332332
end
333333

334334
function _pfmt_g(out::IO, fs::FormatSpec, x::AbstractFloat)
335-
# number decomposition
336-
ax = abs(x)
337-
if 1.0e-4 <= ax < 1.0e6
338-
_pfmt_f(out, fs, x)
335+
# Branch according to the exponent
336+
expnt = floor(Int, log10(abs(x)) )
337+
if -4 <= expnt < fs.prec
338+
newprec = fs.prec - expnt - 1
339+
_pfmt_f(out, FormatSpec(fs ;prec=newprec), x)
339340
else
340-
_pfmt_e(out, fs, x)
341+
newprec = fs.prec - 1
342+
_pfmt_e(out, FormatSpec(fs ;prec=newprec), x)
341343
end
342344
end
343345

0 commit comments

Comments
 (0)