Skip to content

Commit

Permalink
Fix Number#humanize printing of (-)Infinity and NaN (crystal-la…
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlan authored Oct 18, 2024
1 parent a088858 commit 126f037
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions spec/std/humanize_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ describe Number do
it { assert_prints 1.0e+34.humanize, "10,000Q" }
it { assert_prints 1.0e+35.humanize, "100,000Q" }

it { assert_prints Float32::INFINITY.humanize, "Infinity" }
it { assert_prints (-Float32::INFINITY).humanize, "-Infinity" }
it { assert_prints Float32::NAN.humanize, "NaN" }

it { assert_prints Float64::INFINITY.humanize, "Infinity" }
it { assert_prints (-Float64::INFINITY).humanize, "-Infinity" }
it { assert_prints Float64::NAN.humanize, "NaN" }

it { assert_prints 1_234.567_890_123.humanize(precision: 2, significant: false), "1.23k" }
it { assert_prints 123.456_789_012_3.humanize(precision: 2, significant: false), "123.46" }
it { assert_prints 12.345_678_901_23.humanize(precision: 2, significant: false), "12.35" }
Expand Down
2 changes: 1 addition & 1 deletion src/humanize.cr
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ struct Number
#
# See `Int#humanize_bytes` to format a file size.
def humanize(io : IO, precision = 3, separator = '.', delimiter = ',', *, base = 10 ** 3, significant = true, &prefixes : (Int32, Float64) -> {Int32, _} | {Int32, _, Bool}) : Nil
if zero?
if zero? || (responds_to?(:infinite?) && self.infinite?) || (responds_to?(:nan?) && self.nan?)
digits = 0
else
log = Math.log10(abs)
Expand Down

0 comments on commit 126f037

Please sign in to comment.