to_d to always return 2 decimals places in ruby
In addition to mcfinnigan's answer, you can also use the following to get 2 decimal places
'%.2f' % 500 # "500.00"
This use case is known as the string format operator
Since you are using Rails and this seems to be related to a view, there's number_with_precision
:
number_with_precision(500, precision: 2)#=> "500.00"I18n.locale = :denumber_with_precision(500, precision: 2)#=> "500,00"
For currencies I'd suggest number_to_currency
:
number_to_currency(500)#=> "$500.00"
Here's a hint. 500.00 is a representation of the number 500.0
Specifically, sprintf will help you:
irb(main):004:0> sprintf "%.2f", 500.0=> "500.00"