diff --git a/base/ryu/exp.jl b/base/ryu/exp.jl index cf1fe23105b8c..6356206971f8b 100644 --- a/base/ryu/exp.jl +++ b/base/ryu/exp.jl @@ -213,7 +213,7 @@ roundPos = pos while true roundPos -= 1 - if roundPos == (startpos - 1) || buf[roundPos] == UInt8('-') + if roundPos == (startpos - 1) || buf[roundPos] == UInt8('-') || (plus && buf[roundPos] == UInt8('+')) || (space && buf[roundPos] == UInt8(' ')) buf[roundPos + 1] = UInt8('1') e += 1 break diff --git a/base/ryu/fixed.jl b/base/ryu/fixed.jl index 4be1b3741832e..14713111a95a1 100644 --- a/base/ryu/fixed.jl +++ b/base/ryu/fixed.jl @@ -166,7 +166,7 @@ dotPos = 1 while true roundPos -= 1 - if roundPos == (startpos - 1) || (buf[roundPos] == UInt8('-')) + if roundPos == (startpos - 1) || (buf[roundPos] == UInt8('-')) || (plus && buf[roundPos] == UInt8('+')) || (space && buf[roundPos] == UInt8(' ')) buf[roundPos + 1] = UInt8('1') if dotPos > 1 buf[dotPos] = UInt8('0') diff --git a/stdlib/Printf/test/runtests.jl b/stdlib/Printf/test/runtests.jl index d38c90734d4f1..c74739d66ba51 100644 --- a/stdlib/Printf/test/runtests.jl +++ b/stdlib/Printf/test/runtests.jl @@ -141,6 +141,10 @@ end @test Printf.@sprintf("%+ 09.1f", 1.234) == "+000001.2" @test Printf.@sprintf("%+ 09.0f", 1.234) == "+00000001" @test Printf.@sprintf("%+ #09.0f", 1.234) == "+0000001." + + #40303 + @test Printf.@sprintf("%+7.1f", 9.96) == " +10.0" + @test Printf.@sprintf("% 7.1f", 9.96) == " 10.0" end @testset "%e" begin @@ -202,6 +206,10 @@ end @test Printf.@sprintf("%+ 09.1e", 1.234) == "+01.2e+00" @test Printf.@sprintf("%+ 09.0e", 1.234) == "+0001e+00" @test Printf.@sprintf("%+ #09.0e", 1.234) == "+001.e+00" + + #40303 + @test Printf.@sprintf("%+9.1e", 9.96) == " +1.0e+01" + @test Printf.@sprintf("% 9.1e", 9.96) == " 1.0e+01" end @testset "strings" begin