Re: number_format could't work collectly?
| From: | Aziz Saleh | Date: | Wed, 28 Nov 2018 14:25:57 +0000 |
| Subject: | Re: number_format could't work collectly? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-326608@lists.php.net to get a copy of this message | ||
number_format(), as stated by the PHP manual
<http://php.net/manual/en/function.number-format.php>,
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
<http://www.php.net/manual/en/function.strlen.php>
and substr
<http://php.net/manual/en/function.substr.php>
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@vip.qq.com> 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?
>