Skip to content

Commit

Permalink
Merge pull request JuliaLang#37512 from JuliaLang/jq/37507
Browse files Browse the repository at this point in the history
Fix coercion of floats to integer for %d printf specifier
  • Loading branch information
quinnj committed Sep 10, 2020
2 parents 10f5a05 + 73c4890 commit 0d7dc96
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 1,982 deletions.
216 changes: 0 additions & 216 deletions base/grisu/grisu.jl

This file was deleted.

4 changes: 2 additions & 2 deletions stdlib/Printf/src/Printf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ end
leftalign, plus, space, zero, hash, width, prec =
spec.leftalign, spec.plus, spec.space, spec.zero, spec.hash, spec.width, spec.precision
bs = base(T)
arg2 = arg isa AbstractFloat ? Integer(trunc(arg)) : arg
arg2 = arg isa AbstractFloat ? Integer(round(arg)) : arg
n = i = ndigits(arg2, base=bs, pad=1)
x, neg = arg2 < 0 ? (-arg2, true) : (arg2, false)
arglen = n + (neg || (plus | space)) +
Expand Down Expand Up @@ -675,7 +675,7 @@ function plength(f::Spec{T}, x) where {T <: Strings}
end

function plength(f::Spec{T}, x) where {T <: Ints}
x2 = x isa AbstractFloat ? Integer(trunc(x)) : x
x2 = x isa AbstractFloat ? Integer(round(x)) : x
return max(f.width, f.precision + ndigits(x2, base=base(T), pad=1) + 5)
end

Expand Down
2 changes: 2 additions & 0 deletions stdlib/Printf/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ end
@test_throws ArgumentError @sprintf("%s%%%s", "a")
@test @sprintf("%s%%%s", "a", "b") == "a%%b"

# print float as %d uses round(x)
@test @sprintf("%d", 25.5) == "26"
end

@testset "integers" begin
Expand Down
Loading

0 comments on commit 0d7dc96

Please sign in to comment.