note 100405 modified in function.number-format by danbrown
| From: | danbrown@php.net | Date: | Wed, 13 Oct 2010 21:54:27 +0000 |
| Subject: | note 100405 modified in function.number-format by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173197@lists.php.net to get a copy of this message | ||
Caution should be taken with large "string" values. This is due to the precision issues
with converting a string to a float.
For example:
<?php
$org_num = "255173029255255255";
echo "<br>org: ".$org_num;
echo "<br>".number_format($org_num);
?>
returns:
org: 255173029255255255
255,173,029,255,255,264
Here is the function that can be used instead
<?php
function numberFormat($num)
{
return preg_replace("/(?<=\d)(?=(\d{3})+(?!\d))/",",",$num);
}
?>
--was--
Caution should be taken with large "string" values. This is due to the precision issues
with converting a string to a float.
For example:
$org_num = "255173029255255255";
echo "<br>org: ".$org_num;
echo "<br>".number_format($org_num);
returns:
org: 255173029255255255
255,173,029,255,255,264
Here is the function that can be used instead
function numberFormat($num)
{
return preg_replace("/(?<=\d)(?=(\d{3})+(?!\d))/",",",$num);
}
http://php.net/manual/en/function.number-format.php