Skip to content

Commit 00f4194

Browse files
committed
do not trim trailing zeros of integral part
1 parent fbe28e4 commit 00f4194

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

base/ryu/Ryu.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Various options for the output format include:
6464
* `hash`: whether the decimal point should be written, even if no additional digits are needed for precision
6565
* `precision`: minimum number of significant digits to be included in the decimal string; extra `'0'` characters will be added for padding if necessary
6666
* `decchar`: decimal point character to be used
67-
* `trimtrailingzeros`: whether trailing zeros should be removed
67+
* `trimtrailingzeros`: whether trailing zeros of fractional part should be removed
6868
"""
6969
function writefixed(x::T,
7070
precision::Integer,

base/ryu/fixed.jl

+4-1
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ function writefixed(buf, pos, v::T,
6969
buf[pos] = UInt8('0')
7070
pos += 1
7171
end
72+
hasfractional = false
7273
if precision > 0 || hash
7374
buf[pos] = decchar
7475
pos += 1
76+
hasfractional = true
7577
end
7678
if e2 < 0
7779
idx = div(-e2, 16)
@@ -139,6 +141,7 @@ function writefixed(buf, pos, v::T,
139141
if dotPos > 1
140142
buf[dotPos] = UInt8('0')
141143
buf[dotPos + 1] = decchar
144+
hasfractional = true
142145
end
143146
buf[pos] = UInt8('0')
144147
pos += 1
@@ -167,7 +170,7 @@ function writefixed(buf, pos, v::T,
167170
pos += 1
168171
end
169172
end
170-
if trimtrailingzeros
173+
if trimtrailingzeros && hasfractional
171174
while buf[pos - 1] == UInt8('0')
172175
pos -= 1
173176
end

test/ryu.jl

+5-1
Original file line numberDiff line numberDiff line change
@@ -544,10 +544,14 @@ end # Float16
544544
@test Ryu.writefixed(7.018232e-82, 6) == "0.000000"
545545
end
546546

547-
@testset "Consistency of trimtrailingzeros" begin
547+
@testset "Trimming of trailing zeros" begin
548548
@test Ryu.writefixed(0.0, 1, false, false, false, UInt8('.'), true) == "0"
549549
@test Ryu.writefixed(1.0, 1, false, false, false, UInt8('.'), true) == "1"
550550
@test Ryu.writefixed(2.0, 1, false, false, false, UInt8('.'), true) == "2"
551+
552+
@show Ryu.writefixed(1.25e+5, 0, false, false, false, UInt8('.'), true) == "125000"
553+
@show Ryu.writefixed(1.25e+5, 1, false, false, false, UInt8('.'), true) == "125000"
554+
@show Ryu.writefixed(1.25e+5, 2, false, false, false, UInt8('.'), true) == "125000"
551555
end
552556
end # fixed
553557

0 commit comments

Comments
 (0)