Skip to content

Commit

Permalink
fix negative-number engineering formatting (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
isentropic committed Feb 22, 2021
1 parent 6d7a7e5 commit 43e9505
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/ryu.jl
Expand Up @@ -103,11 +103,12 @@ function get_engineering_string(x::AbstractFloat, precision::Integer)
end

buf = IOBuffer()
negative_base_compensation = base_digits[1] == '-' ? 1 : 0
for i in eachindex(base_digits)
if base_digits[i] != '.'
print(buf, base_digits[i])
end
if i == 2 + indices_to_move
if i == 2 + indices_to_move + negative_base_compensation
print(buf, '.')
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Expand Up @@ -36,7 +36,7 @@ end
@test Showoff.format_fixed_scientific(0.012345678, 4, true) == "12.346×10⁻³"
@test Showoff.format_fixed_scientific(0.012345678, 4, false) == "1.2346×10⁻²"
@test Showoff.format_fixed_scientific(-10.0, 4, false) == "-1.0000×10¹"
@test Showoff.format_fixed_scientific(-10.0, 4, false) == "-1.0000×10¹"
@test Showoff.format_fixed_scientific(-10.0, 4, true) == "-10.000×10"
@test Showoff.format_fixed_scientific(-10.0, 4, false)[1:end-5] == @sprintf("%0.4e", -10.0)[1:end-4]
@test Showoff.format_fixed_scientific(1.23456e7, 3, false)[1:end-5] == @sprintf("%0.3e", 1.23456e7)[1:end-4]
@test Showoff.format_fixed_scientific(2.99999999999999956E-16, 2, false) == "3.00×10⁻¹⁶"
Expand Down

0 comments on commit 43e9505

Please sign in to comment.