如何格式化十进制?

时间:2021-07-05 07:29:21

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.

问题是,当我检索00.33时,它输出为0.33。

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

我尝试了一切,但无法让它正常工作。我可以做MySQL的Zerofill,但我真的想避免这种情况。

2 个解决方案

#1


18  

sprintf("%05.2f", 0.33)
# or
"%05.2f" % 0.33

#2


3  

Use printf to format the float as follows:

使用printf格式化浮动,如下所示:

printf( "%05.2f", yourFloat)

For printf examples in C++ see: http://www.cplusplus.com/reference/clibrary/cstdio/printf/.

有关C ++中的printf示例,请参阅:http://www.cplusplus.com/reference/clibrary/cstdio/printf/。

The Ruby docs for sprintf are at: http://www.ruby-doc.org/core-1.9.2/Kernel.html#method-i-sprintf.

sprintf的Ruby文档位于:http://www.ruby-doc.org/core-1.9.2/Kernel.html#method-i-sprintf。

#1


18  

sprintf("%05.2f", 0.33)
# or
"%05.2f" % 0.33

#2


3  

Use printf to format the float as follows:

使用printf格式化浮动,如下所示:

printf( "%05.2f", yourFloat)

For printf examples in C++ see: http://www.cplusplus.com/reference/clibrary/cstdio/printf/.

有关C ++中的printf示例,请参阅:http://www.cplusplus.com/reference/clibrary/cstdio/printf/。

The Ruby docs for sprintf are at: http://www.ruby-doc.org/core-1.9.2/Kernel.html#method-i-sprintf.

sprintf的Ruby文档位于:http://www.ruby-doc.org/core-1.9.2/Kernel.html#method-i-sprintf。