a = '60.950'
b = a.to_f
puts a # => 60.950
puts b # => 60.95
I want to display three digits after the decimal point everytime. How can I force 0 at the end of b?
Use sprintf.
sprintf('%.3f', 60.95)
# => "60.950"
Use the mother of the formatting methods, such as String#% or printf or sprintf, which is Kernel#format
puts format('%.3f', 0)
The others refer to this, and the Kernel#format documentation has the full documentation tables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With