diff --git a/stdlib/Printf/src/Printf.jl b/stdlib/Printf/src/Printf.jl index 8dacee5a1dc1b..b7487db017d45 100644 --- a/stdlib/Printf/src/Printf.jl +++ b/stdlib/Printf/src/Printf.jl @@ -292,7 +292,8 @@ fmt(buf, pos, arg::AbstractFloat, spec::Spec{T}) where {T <: Ints} = bs = base(T) arg2 = toint(arg) n = i = ndigits(arg2, base=bs, pad=1) - x, neg = arg2 < 0 ? (-arg2, true) : (arg2, false) + neg = arg2 < 0 + x = arg2 isa Base.BitSigned ? unsigned(abs(arg2)) : abs(arg2) arglen = n + (neg || (plus | space)) + (T == Val{'o'} && hash ? 1 : 0) + (T == Val{'x'} && hash ? 2 : 0) + (T == Val{'X'} && hash ? 2 : 0) diff --git a/stdlib/Printf/test/runtests.jl b/stdlib/Printf/test/runtests.jl index 3d7929c42e9e0..e80cbe9626823 100644 --- a/stdlib/Printf/test/runtests.jl +++ b/stdlib/Printf/test/runtests.jl @@ -762,6 +762,17 @@ end @test Printf.@sprintf("%20.0X", UInt(3989525555)) == " EDCB5433" @test Printf.@sprintf("%20.X", UInt(0)) == " 0" + # issue #41971 + @test Printf.@sprintf("%4d", typemin(Int8)) == "-128" + @test Printf.@sprintf("%4d", typemax(Int8)) == " 127" + @test Printf.@sprintf("%6d", typemin(Int16)) == "-32768" + @test Printf.@sprintf("%6d", typemax(Int16)) == " 32767" + @test Printf.@sprintf("%11d", typemin(Int32)) == "-2147483648" + @test Printf.@sprintf("%11d", typemax(Int32)) == " 2147483647" + @test Printf.@sprintf("%20d", typemin(Int64)) == "-9223372036854775808" + @test Printf.@sprintf("%20d", typemax(Int64)) == " 9223372036854775807" + @test Printf.@sprintf("%40d", typemin(Int128)) == "-170141183460469231731687303715884105728" + @test Printf.@sprintf("%40d", typemax(Int128)) == " 170141183460469231731687303715884105727" end @testset "%n" begin