Internationalization of floats to display a , instead of - ruby-on-rails

Rails display floating numbers with a . : 5.86 using the american way of displaying numbers. How can i make it display them the european way : 5,86 ?

You can use the number_with_precision helper method from the ActionView::Helpers::NumberHelper module:
number_with_precision(5.86, precision: 2, separator: ',') # => 5,86

Related

Storing multiple currencies in Postgres, using rails

I have a rails app which deals with product prices from different countries.
Storing GBP, and USD is working fine. However, when I start to deal with the CLP (Chilian peso) which has 3 decimal places, it's not storing right.
For example, I am trying to insert 799.990 but when it goes in its storing as 799.99. It is not ideal as there could be a price of 799.99.
I have the price set to t.decimal :price, precision: 19, scale: 4 so this should cover most currencies.
So, my question is there any way to store the price with the trailing 0? Or am I missing something, is there a better way to deal with currencies in rails?
Update:
After searching a bit more on StackOverflow, I think this may be an easier way of dealing with currencies.
number_to_currency locale converting
As 799.99 is same as 799.990 in terms of the amount, you don't need to modify the database column to store the trailing "0". You can simply edit your view to display 3 digits after decimal:
<% if #product.currency == 'CLP' %>
Price: <%= '%.3f' % #product.price %>
<% else %>
<!-- Existing logic -->
<% end %>
Update
Another option is to use sprintf:
<%= sprintf('%.3f', #product.price) %>
As Jagdeep already wrote in his answer, you only need to format the number when you render it to a page.
I would use Rails' number_to_currency helper for this:
CURRENCY_OPTIONS = {
'CLP' => { unit: '$', precision: 3 },
'EUR' => { unit: '€', precision: 2 },
'USD' => { unit: '$', precision: 2 }
}
number_to_currency(price, CURRENCY_OPTIONS[currency])

A simple approach to currency formatting in Rails

I spent most part of today trying to figure out how to give my integer fields a currency format and was bombarded with a myriad of different approaches from using Money, Rails-Money gems to using custom helper methods or locales.
You can achieve this by using the number_to_currency method. Say you
have the following view:
view.html.erb
<ul>
<li><%= client.payment %></li>
</ul>
Assuming the column payments is of type integer, this will print out just that, a number with no formatting. Ex: 5523
Now add the number_to_currency method and specify which currency unit
you'd like to use (can be any)
<ul>
<li><%= number_to_currency(client.payment, :unit => "$") %></li>
</ul>
Now we get something like this: 5.523.00$
The number_to_currency method helper has some options by default, one
of those is that it inverts (as opposed to common practice) the usage
of comma and period. These can be modified by adding the options
:separator and :delimiter and give them the values you'd like as is
below.
<ul>
<li><%= number_to_currency(client.payment, :unit => "$", :separator => ".", :delimiter => ",") %></li>
</ul>
These are the available options for the number_to_currency method helper (RubyOnRailsAPI):
:locale - Sets the locale to be used for formatting (defaults to current locale).
:precision - Sets the level of precision (defaults to 2).
:unit - Sets the denomination of the currency (defaults to “$”).
:separator - Sets the separator between the units (defaults to “.”).
:delimiter - Sets the thousands delimiter (defaults to “,”).
:format - Sets the format for non-negative numbers (defaults to “%u%n”). Fields are %u for the currency, and %n for the number.
:negative_format - Sets the format for negative numbers (defaults to prepending a hyphen to the formatted number given by :format). Accepts the same fields than :format, except %n is here the absolute value of the number.
:raise - If true, raises InvalidNumberError when the argument is invalid.
You can do it a few different ways. Personally I would recommend doing this in a decorator (https://github.com/drapergem/draper) to remove some logic and formatting out of the view.
As mentioned in a prior answer you can use number_to_currency and not use other options to get the correct formatting you are looking for. It can take an integer, float, or string.
number_to_currency 10000
=> "$10,000.00"
Another way is to use money-rails (https://github.com/RubyMoney/money-rails) if you want to deal with a Money object:
money = Money.new(10000)
=> #<Money fractional:10000 currency:USD>
humanized_money_with_symbol money
=> "$10,000.00"
Have you tried using it without any options?
number_to_currency(client.payment)
It should default to $ and has worked fine for me without any options.

How to use Rails number_to_currency options

I'm using Rails and wanted to convert a float to currency. I'm using number_to_currency in a Sidekiq Worker, so I've added include ActionView::Helpers::NumberHelper in the file and where I want the conversion I've added the following:
number_to_currency(value, unit: '', delimeter: '.', separator: ',')
And I wanted the result something like: 1.200,09 but its not working. An example of values I have and want to convert is: 1001.4290000000004 which should be converted to 1.001,43. I've also tried:
number_to_currency(final_total, unit: '', delimeter: ',', separator: '.')
And still doesn't work. I'm getting is 1,200.09. I'm using Rails 4.2.x
What am I missing here?
If you want get result like this 1001.4290000000004 => 1,001.43, you can use this method:
number_with_delimiter(value , delimiter: ",", separator: ".")
But befor u need convert value to the desired value:
value = 1001.4290000000004.round(2) #=> 1001.43
You just have a typo in word delimiter. It should be as follows:
number_to_currency(value, unit: '', delimiter: '.', separator: ',')

Ruby on Rails number_to_currency how to do without Unit [without symbol, $ or other]

How to format my number with the helper number_to_currency, but show it without the currency symbol ($, R$, etc)?
Ex.:
$ 500,78 -> Not what i want
500,78 -> Format i desire
Try using it with the :unit option set to empty string.
number_to_currency(1234567890.50, :unit => "")

Rails saving german price to database

i want to save a price like this "199,99" as a float or decimal to the database.
i have tried to replace the "," with "." in the model with a before filter.
but the price is passed to the before filter as "199.00".
is there a way to convert that in the model class?
I know how to do this in the controller, but how can i do it in the model?
thanks for your help.
I believe the correct way of doing this is keep the db value as it is, and in your view layer create a helper method to display the amount with your preferred formatting
so basic idea is, in your application helper
module ApplicationHelper
#p for price, just to keep things simple
def p(amount)
amount.gsub(".",",")
end
end
and in your view
<%= p(object.price) %>
and one more disadvantage of your approach is , if you save the price as 123,34, you will not be able to do any calculations later.
HTH
You should use number_to_currency helper and appropriate locales
Or if you don't want to mess with locales declare separator explicitly.
number_to_currency(value_from_db, :unit => "$;", :separator => ".", :delimiter => " ", :format => "%u %n")

Resources