Skip to content

Commit

Permalink
Merge pull request #101 from pwim/master
Browse files Browse the repository at this point in the history
Add support for Japanese Yen
  • Loading branch information
semmons99 committed Aug 1, 2011
2 parents 439b7f4 + 4417ba6 commit ed7707e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -208,7 +208,7 @@ implementations.

## Ruby on Rails

Use the `compose_of` helper to let Active Record deal with embedding the money
Use the `composed_of` helper to let Active Record deal with embedding the money
object in your models. The following example requires 2 columns:

``` ruby
Expand Down
2 changes: 1 addition & 1 deletion lib/money/currency.rb
Expand Up @@ -90,7 +90,7 @@ class UnknownCurrency < StandardError; end
:isk => { :priority => 100, :iso_code => "ISK", :name => "Icelandic Króna", :symbol => "kr", :subunit => "Eyrir", :subunit_to_unit => 100, :symbol_first => true, :html_entity => "", :decimal_mark => ",", :thousands_separator => "."},
:jmd => { :priority => 100, :iso_code => "JMD", :name => "Jamaican Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => 100, :symbol_first => true, :html_entity => "$", :decimal_mark => ".", :thousands_separator => ","},
:jod => { :priority => 100, :iso_code => "JOD", :name => "Jordanian Dinar", :symbol => "د.ا", :subunit => "Piastre", :subunit_to_unit => 100, :symbol_first => true, :html_entity => "", :decimal_mark => ".", :thousands_separator => ","},
:jpy => { :priority => 6, :iso_code => "JPY", :name => "Japanese Yen", :symbol => "¥", :subunit => "Sen", :subunit_to_unit => 100, :symbol_first => true, :html_entity => "&#x00A5;", :decimal_mark => ".", :thousands_separator => ","},
:jpy => { :priority => 6, :iso_code => "JPY", :name => "Japanese Yen", :symbol => "¥", :subunit => nil, :subunit_to_unit => 1, :symbol_first => true, :html_entity => "&#x00A5;", :decimal_mark => ".", :thousands_separator => ","},
:kes => { :priority => 100, :iso_code => "KES", :name => "Kenyan Shilling", :symbol => "Sh", :subunit => "Cent", :subunit_to_unit => 100, :symbol_first => true, :html_entity => "", :decimal_mark => ".", :thousands_separator => ","},
:kgs => { :priority => 100, :iso_code => "KGS", :name => "Kyrgyzstani Som", :symbol => nil, :subunit => "Tyiyn", :subunit_to_unit => 100, :symbol_first => false, :html_entity => "", :decimal_mark => ".", :thousands_separator => ","},
:khr => { :priority => 100, :iso_code => "KHR", :name => "Cambodian Riel", :symbol => "៛", :subunit => "Sen", :subunit_to_unit => 100, :symbol_first => false, :html_entity => "&#x17DB;", :decimal_mark => ".", :thousands_separator => ","},
Expand Down
18 changes: 17 additions & 1 deletion lib/money/money/formatting.rb
@@ -1,3 +1,4 @@
# encoding: UTF-8
class Money
module Formatting

Expand Down Expand Up @@ -142,6 +143,7 @@ def decimal_mark
def format(*rules)
# support for old format parameters
rules = normalize_formatting_rules(rules)
rules = localize_formatting_rules(rules)

if cents == 0
if rules[:display_free].respond_to?(:to_str)
Expand Down Expand Up @@ -187,7 +189,12 @@ def format(*rules)
end

if symbol_value && !symbol_value.empty?
formatted = (symbol_position == :before ? "#{symbol_value}#{formatted}" : "#{formatted} #{symbol_value}")
formatted = if symbol_position == :before
"#{symbol_value}#{formatted}"
else
symbol_space = rules[:symbol_after_without_space] ? "" : " "
"#{formatted}#{symbol_space}#{symbol_value}"
end
end

if rules.has_key?(:decimal_mark) and rules[:decimal_mark] and
Expand Down Expand Up @@ -241,4 +248,13 @@ def normalize_formatting_rules(rules)
rules
end
end

def localize_formatting_rules(rules)
if currency.iso_code == "JPY" && I18n.locale == :ja
rules[:symbol] = "円"
rules[:symbol_position] = :after
rules[:symbol_after_without_space] = true
end
rules
end
end
12 changes: 10 additions & 2 deletions spec/money/formatting_spec.rb
Expand Up @@ -130,6 +130,14 @@
end

describe "#format" do
context "Japanese Locale" do
before { @before = I18n.locale; I18n.locale = :ja }
it "should format Japanese currency in Japanese properly" do
Money.new(1000, "JPY").format.should == "1,000円"
end
after { I18n.locale = @before }
end

it "returns the monetary value as a string" do
Money.ca_dollar(100).format.should == "$1.00"
Money.new(40008).format.should == "$400.08"
Expand Down Expand Up @@ -159,7 +167,7 @@
one_thousand["ZWD"].should == "$1,000.00"

# Yen
one_thousand["JPY"].should == 1,000.00"
one_thousand["JPY"].should == 100,000"
one_thousand["CNY"].should == "¥1,000.00"

# Euro
Expand Down Expand Up @@ -261,7 +269,7 @@
one["ZWD"].should == "$1.00"

# Yen
one["JPY"].should == 1.00"
one["JPY"].should == 100"
one["CNY"].should == "¥1.00"

# Euro
Expand Down

0 comments on commit ed7707e

Please sign in to comment.