是否存在可以解析以下所有数字的现有解决方案?
"300.00"
"2.300,00"
"2,300.00"
尝试使用money
宝石:
$ gem install money
然后你可以这样做:
require 'money'
test1 = Money.parse("300.00")
test2 = Money.parse("2.300,00")
test3 = Money.parse("2,300.00")
test1.currency # #<Money::Currency id: usd, priority: 1, symbol_first: true, thousands_separator: ,, html_entity: $, decimal_mark: ., name: United States Dollar, symbol: $, subunit_to_unit: 100, iso_code: USD, iso_numeric: 840, subunit: Cent>
test1.amount # 300.0
test1.dollars # 300.0
test1.cents # 30000
test1.currency_as_string # USD
test1.separator # .
test1.thousands_separator # ,
test1.delimiter # ,
编辑:旧money
宝石分为两部分:money
和monetize
. 新类仅处理对象money
之间的货币创建、操作和转换。money
要将对象(包括字符串)解析为money
对象,您应该改用monetize
gem:
$ gem install monetize
Monetize.parse("USD 100")
Monetize.parse("£100")
Monetize.parse_collection("€80, $100")