Skip to content

Commit ac44caf

Browse files
authored
Merge pull request #47 from jmkuhn/pull-request/d2df85fa
Round before calculating length (Fixes #46)
2 parents 8f5b65b + d2df85f commit ac44caf

File tree

2 files changed

+4
-7
lines changed

2 files changed

+4
-7
lines changed

src/fmtcore.jl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,6 @@ function _pfmt_float(out::IO, sch::Char, zs::Integer, intv::Real, decv::Real, pr
149149
_repwrite(out, '0', zs)
150150
end
151151
idecv = round(Integer, decv * exp10(prec))
152-
if idecv == exp10(prec)
153-
intv += 1
154-
idecv = 0
155-
end
156152
# print integer part
157153
if intv == 0
158154
write(out, '0')
@@ -173,10 +169,10 @@ end
173169

174170
function _pfmt_f(out::IO, fs::FormatSpec, x::AbstractFloat)
175171
# separate sign, integer, and decimal part
176-
ax = abs(x)
172+
rax = round(abs(x), fs.prec)
177173
sch = _signchar(x, fs.sign)
178-
intv = trunc(Integer, ax)
179-
decv = ax - intv
174+
intv = trunc(Integer, rax)
175+
decv = rax - intv
180176

181177
# calculate length
182178
xlen = _ndigits(intv, _Dec()) + 1 + fs.prec

test/fmtspec.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ fs = FormatSpec("d")
143143

144144
@test fmt(".2f", 0.999) == "1.00"
145145
@test fmt(".2f", 0.996) == "1.00"
146+
@test fmt("6.2f", 9.999) == " 10.00"
146147
# Floating point error can upset this one (i.e. 0.99500000 or 0.994999999)
147148
@test (fmt(".2f", 0.995) == "1.00" || fmt(".2f", 0.995) == "0.99")
148149
@test fmt(".2f", 0.994) == "0.99"

0 commit comments

Comments
 (0)