Search code examples
ruby-on-railsnumber-formatting

How to pad zeros to a decimal result


I am trying to pad zeros to a decimal result.

Is there a one-line code to do this in the view?

<%= "%00.1f" %  (29/10.0) %>

expected: 02.9

the above returned: 2.9


Solution

  • For those who want to pad zeroes to floating or decimal values, just provide the number of characters to be printed.

    "%04.1f" %  (29/10.0) 
    

    4 means four characters will be printed including the decimal and the decimal point.