Round的结果格式

时间:2012-11-01 22:34:06

标签: php

  

可能重复:
  how to display number in a certain format in php

echo round(10.1232, 2); // return 10.12

这工作正常,但是:

echo round(10, 2); // return 10

我想收到 10.00 。我该怎么做?

2 个答案:

答案 0 :(得分:5)

使用number_format()

echo number_format(10, 2, '.', '');

答案 1 :(得分:1)

PHP也有printf

printf( "%.2f\n", round(10.1232, 2) );
printf( "%.2f\n", round(10, 2) );

事实上,舍入由printf处理......所以这应该是相同的:

printf( "%.2f\n", 10.1232 );
printf( "%.2f\n", 10 );