Reputation: 1348
I have a number_field thus:
<%= ff.number_field :total_cost_savings, {class: 'form-control number', :step => 0.01} %>
I enter the value 1.1, save and refresh and it displays the value "1.100000023841858". I understand that this is because the underlying database field is a float and that I could change the database field to be a decimal, but isn't there some way to control the display formatting of the number_field instead?
Upvotes: 0
Views: 746
Reputation: 1348
The answer is:
<%= ff.number_field :total_cost_savings, {value: number_with_precision(ff.object.total_cost_savings, precision: 2), class: 'form-control number', :step => 0.01} %>
Upvotes: 1
Reputation: 101
For prices you can do like this:
f.number_field :total_cost_savings, value: number_to_currency(f.object.total_cost_savings, delimiter: '', unit: ''), step: 0.01
Also, you can overwrite the active record getter, by adding this to your model file:
def total_cost_savings
read_attribute(:total_cost_savings).round(2)
end
Upvotes: 0