Is there an already existing solution that can parse all the numbers below?
"300.00"
"2.300,00"
"2,300.00"
The toLocaleString() method returns a string with a language-sensitive representation of this date. In implementations with Intl. DateTimeFormat API support, this method simply calls Intl. DateTimeFormat .
myNumber = nf. parse(myString); Use getInstance or getNumberInstance to get the normal number format. Use getIntegerInstance to get an integer number format.
NumberFormat is a Java class for formatting and parsing numbers. With NumberFormat , we can format and parse numbers for any locale. NumberFormat allows us to round values, set decimal separators, set the number of fraction digits, or format values according to a specific locale.
Try using the money
gem:
$ gem install money
Then you can do:
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 # ,
EDIT: the old money
gem has split into two parts: money
and monetize
. The new money
class only handles creating, manipulating and converting currencies between money
objects.
To parse objects (including strings) into money
objects, you should use the monetize
gem instead:
$ gem install monetize
Monetize.parse("USD 100")
Monetize.parse("£100")
Monetize.parse_collection("€80, $100")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With