Skip to content

Commit

Permalink
Change to handle string number in ordinal conversion and support trai…
Browse files Browse the repository at this point in the history
…ling zeros option
  • Loading branch information
piotrmurach committed Dec 16, 2019
1 parent 211134a commit 9fcfbe8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/strings/numeral.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def ordinalize(num, **options)
if options[:short]
num.to_s + short_ordinalize(num)
else
decimals = (num.to_i.abs != num.abs)
decimals = (num.to_i.abs != num.to_f.abs)
sentence = convert_numeral(num, **options)
separators = [AND, POINT, options[:separator]].compact

Expand Down
12 changes: 12 additions & 0 deletions spec/unit/ordinalize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
1.23 => "first and twenty three hundredths",
12.003 => "twelfth and three thousandths",
0.001 => "zeroth and one thousandth",
12.100 => "twelfth and one tenth",
12.000 => "twelfth",
123.456 => "one hundred twenty third and four hundred fifty six thousandths",
-114.5678 => "negative one hundred fourteenth and five thousand, six hundred seventy eight ten-thousandths",
1234.567 => "one thousand, two hundred thirty fourth and five hundred sixty seven thousandths",
Expand All @@ -52,6 +54,8 @@
0.21 => "zeroth point two one",
1.23 => "first point two three",
12.003 => "twelfth point zero zero three",
12.100 => "twelfth point one",
12.000 => "twelfth",
123.456 => "one hundred twenty third point four five six",
-114.5678 => "negative one hundred fourteenth point five six seven eight",
1234.567 => "one thousand, two hundred thirty fourth point five six seven",
Expand All @@ -71,4 +75,12 @@
expect(Strings::Numeral.ordinalize(1_234.567, separator: "dot")).
to eq("one thousand, two hundred thirty fourth dot five hundred sixty seven thousandths")
end

it "removes trailing zeros for strings to match number behaviour" do
expect(Strings::Numeral.ordinalize("12.100")).to eq("twelfth and one tenth")
end

it "keeps trailing zeros for strings when :trailing_zeros is set to true" do
expect(Strings::Numeral.ordinalize("12.100", trailing_zeros: true)).to eq("twelfth and one hundred thousandths")
end
end

0 comments on commit 9fcfbe8

Please sign in to comment.