Skip to content

Commit

Permalink
Change to allow custom delimiter in cardinal numerals
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Dec 15, 2019
1 parent 3152f33 commit 3367e24
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/strings/numeral.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,12 @@ def short_ordinalize(num, short: false)
# the number as numeral
#
# @api private
def convert_numeral(num, delimiter: ",", decimal: :fraction)
def convert_numeral(num, delimiter: ", ", decimal: :fraction)
negative = num < 0
n = num.to_i.abs
decimals = (n != num.abs)

sentence = convert_to_words(n).join("#{delimiter} ")
sentence = convert_to_words(n).join(delimiter)

if sentence.empty?
sentence = ZERO
Expand All @@ -248,13 +248,13 @@ def convert_numeral(num, delimiter: ",", decimal: :fraction)
# @return [String]
#
# @api private
def convert_decimals(num, delimiter: ",", decimal: :fraction)
def convert_decimals(num, delimiter: ", ", decimal: :fraction)
dec_num = num.to_s.split(".")[1]

case decimal
when :fraction
unit = DECIMAL_SLOTS[dec_num.to_s.length - 1]
convert_to_words(dec_num.to_i).join("#{delimiter} ") + " " + unit
convert_to_words(dec_num.to_i).join(delimiter) + " " + unit
when :digit
dec_num.chars.map do |n|
(v = convert_tens(n.to_i)).empty? ? ZERO : v
Expand Down
5 changes: 5 additions & 0 deletions spec/unit/cardinalize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@
Strings::Numeral.cardinalize(123.45, decimal: :unknown)
}.to raise_error(Strings::Numeral::Error, "Unknown decimal option ':unknown'")
end

it "allows to change a thousand's delimiter" do
expect(Strings::Numeral.cardinalize(1_234_567, delimiter: " and ")).
to eq("one million and two hundred thirty four thousand and five hundred sixty seven")
end
end

0 comments on commit 3367e24

Please sign in to comment.