Skip to content

Commit

Permalink
allow true for thousands separator
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklozon committed Jun 1, 2021
1 parent ca59ced commit 18cae53
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/money/money/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,14 @@ class Formatter
# the currency should be delimited by the specified character or ','
#
# @example
# # If false is specified, no thousands_separator is used.
# # If a falsey value is specified, no thousands_separator is used.
# Money.new(100000, "USD").format(thousands_separator: false) #=> "1000.00"
# Money.new(100000, "USD").format(thousands_separator: nil) #=> "1000.00"
# Money.new(100000, "USD").format(thousands_separator: "") #=> "1000.00"
#
# # If true is specified, the locale or default thousands_separator is used.
# Money.new(100000, "USD").format(thousands_separator: true) #=> "1,000.00"
#
# # If a string is specified, it's value is used.
# Money.new(100000, "USD").format(thousands_separator: ".") #=> "$1.000.00"
#
Expand Down Expand Up @@ -241,7 +244,13 @@ def to_s
end

def thousands_separator
lookup :thousands_separator
val = lookup :thousands_separator

if val == true
val = (Money.locale_backend && Money.locale_backend.lookup(:thousands_separator, currency)) || DEFAULTS[key]
end

val
end

def decimal_mark
Expand Down
9 changes: 9 additions & 0 deletions spec/money/formatting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@

describe ":thousands_separator option" do
specify "(thousands_separator: a thousands_separator string) works as documented" do
expect(Money.us_dollar(100000).format(thousands_separator: ".")).to eq "$1.000.00"
expect(Money.us_dollar(100000).format(thousands_separator: ".")).to eq "$1.000.00"
expect(Money.us_dollar(200000).format(thousands_separator: "")).to eq "$2000.00"
end
Expand All @@ -456,6 +457,10 @@
expect(Money.us_dollar(200000).format(thousands_separator: nil)).to eq "$2000.00"
end

specify "(thousands_separator: true) works as documented" do
expect(Money.us_dollar(100000).format(thousands_separator: true)).to eq "$1,000.00"
end

specify "(delimiter: a delimiter string) works as documented" do
expect(Money.us_dollar(100000).format(delimiter: ".")).to eq "$1.000.00"
expect(Money.us_dollar(200000).format(delimiter: "")).to eq "$2000.00"
Expand All @@ -466,6 +471,10 @@
expect(Money.us_dollar(200000).format(delimiter: nil)).to eq "$2000.00"
end

specify "(delimiter: true) works as documented" do
expect(Money.us_dollar(100000).format(delimiter: true)).to eq "$1,000.00"
end

it "defaults to ',' if currency isn't recognized" do
expect(Money.new(100000, "ZWD").format).to eq "$1,000.00"
end
Expand Down

0 comments on commit 18cae53

Please sign in to comment.