diff --git a/base/ryu/Ryu.jl b/base/ryu/Ryu.jl index 1d260fe9b3696..81d1c41f4c19f 100644 --- a/base/ryu/Ryu.jl +++ b/base/ryu/Ryu.jl @@ -64,7 +64,7 @@ Various options for the output format include: * `hash`: whether the decimal point should be written, even if no additional digits are needed for precision * `precision`: minimum number of significant digits to be included in the decimal string; extra `'0'` characters will be added for padding if necessary * `decchar`: decimal point character to be used - * `trimtrailingzeros`: whether trailing zeros should be removed + * `trimtrailingzeros`: whether trailing zeros of fractional part should be removed """ function writefixed(x::T, precision::Integer, diff --git a/base/ryu/fixed.jl b/base/ryu/fixed.jl index 5c780aa7acbf8..e0085f5c66dab 100644 --- a/base/ryu/fixed.jl +++ b/base/ryu/fixed.jl @@ -69,9 +69,11 @@ function writefixed(buf, pos, v::T, buf[pos] = UInt8('0') pos += 1 end + hasfractional = false if precision > 0 || hash buf[pos] = decchar pos += 1 + hasfractional = true end if e2 < 0 idx = div(-e2, 16) @@ -139,6 +141,7 @@ function writefixed(buf, pos, v::T, if dotPos > 1 buf[dotPos] = UInt8('0') buf[dotPos + 1] = decchar + hasfractional = true end buf[pos] = UInt8('0') pos += 1 @@ -167,7 +170,7 @@ function writefixed(buf, pos, v::T, pos += 1 end end - if trimtrailingzeros + if trimtrailingzeros && hasfractional while buf[pos - 1] == UInt8('0') pos -= 1 end diff --git a/test/ryu.jl b/test/ryu.jl index d8d3c17752f63..d5a8df24bd03b 100644 --- a/test/ryu.jl +++ b/test/ryu.jl @@ -544,10 +544,14 @@ end # Float16 @test Ryu.writefixed(7.018232e-82, 6) == "0.000000" end - @testset "Consistency of trimtrailingzeros" begin + @testset "Trimming of trailing zeros" begin @test Ryu.writefixed(0.0, 1, false, false, false, UInt8('.'), true) == "0" @test Ryu.writefixed(1.0, 1, false, false, false, UInt8('.'), true) == "1" @test Ryu.writefixed(2.0, 1, false, false, false, UInt8('.'), true) == "2" + + @show Ryu.writefixed(1.25e+5, 0, false, false, false, UInt8('.'), true) == "125000" + @show Ryu.writefixed(1.25e+5, 1, false, false, false, UInt8('.'), true) == "125000" + @show Ryu.writefixed(1.25e+5, 2, false, false, false, UInt8('.'), true) == "125000" end end # fixed