Skip to content

Commit

Permalink
Merge pull request #40755 from bicycle1885/ryu-trimzeros
Browse files Browse the repository at this point in the history
do not trim trailing zeros of integral part
  • Loading branch information
quinnj authored May 11, 2021
2 parents 67186f4 + 00f4194 commit 79b8af2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion base/ryu/Ryu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion base/ryu/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion test/ryu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 79b8af2

Please sign in to comment.