Skip to content

Commit 58c0ad2

Browse files
aplavinScottPJones
authored andcommitted
Don't print decimal point when unnecessary: formatting PR #101
1 parent bbbd5ef commit 58c0ad2

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

src/fmtcore.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,9 @@ function _pfmt_float(out::IO, sch::AbstractChar, zs::Integer, intv::Real, decv::
147147
else
148148
_pfmt_intdigits(out, intv, _Dec())
149149
end
150-
# print decimal point
151-
print(out, '.')
152150
# print decimal part
153151
if prec > 0
152+
print(out, '.')
154153
nd = _ndigits(idecv, _Dec())
155154
nd < prec && _repprint(out, '0', prec - nd)
156155
_pfmt_intdigits(out, idecv, _Dec())
@@ -165,7 +164,7 @@ function _pfmt_f(out::IO, fs::FormatSpec, x::AbstractFloat)
165164
decv = rax - intv
166165

167166
# calculate length
168-
xlen = _ndigits(intv, _Dec()) + 1 + fs.prec
167+
xlen = _ndigits(intv, _Dec()) + ifelse(fs.prec > 0, fs.prec + 1, 0)
169168
sch != '\0' && (xlen += 1)
170169

171170
# print

test/fmtspec.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ end
149149
@test pyfmt("8.2f", -8.376) == " -8.38"
150150
@test pyfmt("<8.2f", -8.376) == "-8.38 "
151151
@test pyfmt(">8.2f", -8.376) == " -8.38"
152+
@test pyfmt(".0f", 8.376) == "8"
152153

153154
@test pyfmt("<08.2f", 8.376) == "00008.38"
154155
@test pyfmt(">08.2f", 8.376) == "00008.38"
@@ -201,6 +202,10 @@ end
201202
@test pyfmt(".1e", 0.6) == "6.0e-01"
202203
@test pyfmt(".1e", 0.9) == "9.0e-01"
203204

205+
# issue #61 (from Formatting.jl)
206+
@test pyfmt("1.0e", 1e-21) == "1e-21"
207+
@test pyfmt("1.1e", 1e-21) == "1.0e-21"
208+
204209
@test pyfmt("10.2e", 1.2e100) == " 1.20e+100"
205210
@test pyfmt("11.2e", BigFloat("1.2e1000")) == " 1.20e+1000"
206211
@test pyfmt("11.2e", BigFloat("1.2e-1000")) == " 1.20e-1000"

0 commit comments

Comments
 (0)