Ruby String with Usd "Money" Converted to Number

Ruby: Convert dollar (String) to cents (Integer)

You can use String#to_r ("to rational") to avoid round-off error.

def dollars_to_cents(dollars)
(100 * dollars.to_r).to_i
end

dollars_to_cents("12")
#=> 1200
dollars_to_cents("10.25")
#=> 1025
dollars_to_cents("-10.25")
#=> -1025
dollars_to_cents("-0")
#=> 0

Currency to number conversion rails

Jorge's answer is good, but I think you'll need to know how the currency is entered. This will require whitelisting more than black listing.

def currency_to_number currency
currency.to_s.gsub(/[$,]/,'').to_f
end

Convert & store String into currency values

If you're sure that first number is USD and second number is AED and the order won't change then:

str = "$6,178.50 USD / 22,693.01 AED"

usa_price, aed_price = str.scan(/\d{1,2}?,?\d{1,3}\.\d{2}/)

#=> usa_price = 6,178.50, aed_price = 22,693.01

How to parse through complex currency value from a string Ruby

Just need to extract the unit like million:

s = '"$1,950,000"          #=> "1950000
"$750,000" #=> "750000"
"$1,433,000[2]" #=> "1433000"
"$1.3 million" #=> "1300000"
"$2.183 million[4]" #=> "2183000"
"US$1,644,736 (est.)" #=> "1644736"'

s.scan(/[\$£](\d{1,3}(,\d{3})*(\.\d*)?)( hundred| thousand| million| billion)?/).each do |m|
number = m[0].gsub(',', '').to_f
unit = m[3]
multiplier = 1

if unit == ' hundred' then
multiplier = 1e2
elsif unit == ' thousand' then
multiplier = 1e3
elsif unit == ' million' then
multiplier = 1e6
elsif unit == ' billion' then
multiplier = 1e9
end

puts number * multiplier
end

prints

1950000.0
750000.0
1433000.0
1300000.0
2183000.0
1644736.0

How do I convert a decimal to string value for dollars and cents in ruby?

You can accomplish that with this little bit of Ruby code:

fmt = "%05.2f" % cost
cost_dollars, cost_cents = fmt.split '.'

Convert cents into dollar string in Ruby without use of BigDecimal

Here's a one-line method that also simply uses string manipulation thereby completely bypassing the numeric issues:

cents.rjust(3, "0").insert(-3, ".")

How to convert an integer to currency and display the correct cents

Stripe stores values as cents. From the docs:

Zero-decimal currencies


All API requests expect amounts to be provided in a currency’s smallest unit. For example, to charge 10 USD, provide an amount value of 1000 (i.e., 1000 cents).

I assume that the API responses work alike.

To get the correct output, you have to divide the USD value by 100, e.g.:

<%= number_to_currency(@service.amount.fdiv(100)) %>

There's also the Money gem which might be a better alternative. It stores both, the value (as cents) and its currency, and comes with formatting:

require 'money'

money = Money.new(@service.amount, @service.currency)
#=> #<Money fractional:2895 currency:USD>

money.format
#=> "$28.95"

What is the best method of handling currency/money?

You'll probably want to use a DECIMAL type in your database. In your migration, do something like this:

# precision is the total number of digits
# scale is the number of digits to the right of the decimal point
add_column :items, :price, :decimal, :precision => 8, :scale => 2

In Rails, the :decimal type is returned as BigDecimal, which is great for price calculation.

If you insist on using integers, you will have to manually convert to and from BigDecimals everywhere, which will probably just become a pain.

As pointed out by mcl, to print the price, use:

number_to_currency(price, :unit => "€")
#=> €1,234.01


Related Topics



Leave a reply



Submit