Reputation: 199
How can I set both precision and delimiter for number in Rails?
For example, I have a float number 1000.0 and want to get 1 000,00
My locale file:
number:
format:
delimiter: ' '
precision: 2
separator: ','
I used helpers methods:
number_with_precision returns
1000,00
number_with_delimiter returns
1 000,0
both number_with_precision and number_with_delimiter return
1 000,0
localize returns
Object must be a Date, DateTime or Time object. 1000.0 given.
Upvotes: 6
Views: 2906
Reputation: 134
Use number_to_currency and strip the leading whitespace
number_to_currency(1000.0, unit: '')[1..-1]
=>
"1 000,00"
Upvotes: 1
Reputation: 2142
number_with_precision has a delimiter and separator option.
<%= number_with_precision(1000.0, precision: 2, delimiter: ' ', separator: ',') %>
=> 1 000,00
Upvotes: 9