Skip to content

Commit bb90d64

Browse files
authored
Merge pull request #36819 from JuliaLang/jq/36234
Fix compact float printing when output contains exactly 6 digits
2 parents 3b67e47 + ada82ac commit bb90d64

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

base/ryu/shortest.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,8 @@ end
330330
olength = decimallength(output)
331331
exp_form = true
332332
pt = nexp + olength
333-
if -4 < pt <= (precision == -1 ? (T == Float16 ? 3 : 6) : precision)
333+
if -4 < pt <= (precision == -1 ? (T == Float16 ? 3 : 6) : precision) &&
334+
!(pt >= olength && abs(mod(x + 0.05, 10^(pt - olength)) - 0.05) > 0.05)
334335
exp_form = false
335336
if pt <= 0
336337
buf[pos] = UInt8('0')

test/numbers.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,14 +415,16 @@ end
415415
@test repr(-NaN) == "NaN"
416416
@test repr(Float64(pi)) == "3.141592653589793"
417417
# issue 6608
418-
@test sprint(show, 666666.6, context=:compact => true) == "666667.0"
418+
@test sprint(show, 666666.6, context=:compact => true) == "6.66667e5"
419419
@test sprint(show, 666666.049, context=:compact => true) == "666666.0"
420420
@test sprint(show, 666665.951, context=:compact => true) == "666666.0"
421421
@test sprint(show, 66.66666, context=:compact => true) == "66.6667"
422-
@test sprint(show, -666666.6, context=:compact => true) == "-666667.0"
422+
@test sprint(show, -666666.6, context=:compact => true) == "-6.66667e5"
423423
@test sprint(show, -666666.049, context=:compact => true) == "-666666.0"
424424
@test sprint(show, -666665.951, context=:compact => true) == "-666666.0"
425425
@test sprint(show, -66.66666, context=:compact => true) == "-66.6667"
426+
@test sprint(show, -498796.2749933266, context=:compact => true) == "-4.98796e5"
427+
@test sprint(show, 123456.78, context=:compact=>true) == "1.23457e5"
426428

427429
@test repr(1.0f0) == "1.0f0"
428430
@test repr(-1.0f0) == "-1.0f0"

0 commit comments

Comments
 (0)