5

是否存在可以解析以下所有数字的现有解决方案?

"300.00"
"2.300,00"
"2,300.00"
4

1 回答 1

6

尝试使用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宝石分为两部分:moneymonetize. 新类仅处理对象money之间的货币创建、操作和转换。money

要将对象(包括字符串)解析为money对象,您应该改用monetizegem

$ gem install monetize

Monetize.parse("USD 100")
Monetize.parse("£100")
Monetize.parse_collection("€80, $100")
于 2013-01-29T00:19:02.020 回答