diff --git a/src/fmtcore.jl b/src/fmtcore.jl index 19501a5..c54a641 100644 --- a/src/fmtcore.jl +++ b/src/fmtcore.jl @@ -154,6 +154,19 @@ function _pfmt_i(out::IO, fs::FormatSpec, x::Integer, op::Op) where {Op} postpad == 0 || _repprint(out, fs.fill, postpad) end +function _truncval(v) + try + return trunc(Integer, v) + catch e + e isa InexactError || rethrow(e) + end + try + return trunc(Int128, v) + catch e + e isa InexactError || rethrow(e) + end + trunc(BigInt, v) +end ### print floating point numbers @@ -162,7 +175,7 @@ function _pfmt_f(out::IO, fs::FormatSpec, x::AbstractFloat) prec = fs.prec rax = round(abs(x); digits = prec) sch = _signchar(x, fs.sign) - intv = trunc(Integer, rax) + intv = _truncval(rax) decv = rax - intv # calculate length diff --git a/test/fmtspec.jl b/test/fmtspec.jl index 95035a7..90b52a7 100644 --- a/test/fmtspec.jl +++ b/test/fmtspec.jl @@ -275,6 +275,9 @@ end # Floating point error can upset this one (i.e. 0.99500000 or 0.994999999) @test (pyfmt(".2f", 0.995) == "1.00" || pyfmt(".2f", 0.995) == "0.99") @test pyfmt(".2f", 0.994) == "0.99" + + # issue #102 (from Formatting.jl) + @test pyfmt("15.6f", 1e20) == "100000000000000000000.000000" end @testset "Format floating point (e)" begin