views:

310

answers:

2
+2  Q: 

Format a Decimal

Hi,

I need to format a decimal like this:

00.33

11.24

05.22

The problem is that when I retrieve 00.33 it outputs as 0.33.

I have tried everything and can't get it to work correctly. I could do MySQL's Zerofill but I'm really trying to avoid that.

Any suggestions?

Thanks!

+2  A: 

Use printf to format the float as follows

printf( "%05.2f", yourFloat)

for printf examples in C++ see http://www.cplusplus.com/reference/clibrary/cstdio/printf/
The ruby docs for sprintf are at http://www.ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/function.html#sprintf

edit: corrected format string

Jonathan Fingland
+5  A: 
sprintf("%05.2f", 0.33)
# or
"%05.2f" % 0.33
Chuck