Re: number_format could't work collectly?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On November 28, 2018 at 8:25 AM Aziz Saleh <azizsaleh@xxxxxxxxx> wrote:

number_format(), as stated by the PHP manual, converts a float into a string with the thousands groups with commas.

$number = '1562798794365432135246'; var_dump($number); // string(22) "1562798794365432135246" var_dump(number_format($number)); // string(29) "1,562,798,794,365,432,233,984" 

If you're trying to cast a string to an integer, you can do so like this:

$number = (int) $number;

However, be careful, since the largest possible integer value in PHP is 2147483647 on a 32-bit system and 9223372036854775807 on a 64-bit system. If you try to cast a string like the one above to int on a 32-bit system, you'll assign $number to 2147483647 rather than the value you intend!

The number_format() function takes a float as an argument, which has similar issues. When you pass a string as an argument to number_format(), it is internally converted to a float. Floats are a little more complicated than integers. Instead of having a hard upper bound like an integer value, floats progressively lose precision in the least significant digits, making those last few places incorrect.

So, unfortunately, if you need to format long strings like this you'll probably need to write your own function. If you only need to add commas in the thousands places, this should be easy - just use strlen and substr to get every set of three characters from the end of string and create a new string with commas in between.

 

 

SOURCE: https://stackoverflow.com/a/6936191/1935500


On Wed, Nov 28, 2018 at 9:06 AM 陈晓熳 < zsxx51@xxxxxxxxxx> wrote:
Hi,everyone.

I have a problem about number_format.

There is a bit variable like :
0101100111010110101001110001110000010101000000000000110000000001

when I just echo ,that is:
6473545253444914177

work well,but when I use number_format :

number_format(bindec('0101100111010110101001110001110000010101000000000000110000000001'),'0','','');

It echo:
6473545253444914176

Can somebody could tell me why?

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux