开发者

Format number in PHP

I would like to format number with decimal开发者_运维问答 places to format without decimal places, example: 146.5 to 146500000. How can i achieve this in PHP? Thank you.


Multiply by 100000?

Couldn't be that easy.


There are a couple ways to do this:

  1. Multiply the number by some power of 10
  2. User number_format (docs) to add the zeros, then use str_replace (docs) to remove the decimal point


$formatted_number = preg_replace('/\./', '', sprintf('%06f', $number));


Maybe you meant 1465.00000 then you could use floor($num);


Floating point numbers can be tricksy, be sure to do any multiplication with an extended math library. Check out bcmul() from the BC Math Library for more info.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜