note 83438 added to function.number-format
| From: | teamatglossworddotbiz at osu1 dot php dot net | Date: | Mon, 26 May 2008 13:45:34 +0000 |
| Subject: | note 83438 added to function.number-format | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-139794@lists.php.net to get a copy of this message | ||
It's pity that the function supports one-byte characters only for "thousands
separator".
For English it works well but for French and Russian it makes problem.
Developers should use character   or chr(0xA0) as "thousands separator" to avoid
the number of being moved to a new line in HTML-documents. But number_format() didn't
understand that symbol.
To solve the problem, we wrote our own variant of number_format():
<?php
/* Replacement for number_format() */
function my_number_format($n, $decimals = 0, $dec_point = ',', $thousands_sep =
' ')
{
$b = explode('.', $n);
$rn = '';
$l = strlen($b[0]);
/* Reverse string */
for ($i = $l; $i > 3; $i -= 3)
{
$rn = $thousands_sep . substr($b[0], $i - 3, 3) . $rn;
}
/* sprintf() used to correct 0.79 to 0.790 */
/* str_replace() used to correct decimals */
/* str_repeat() used to correct decimals */
return substr($b[0], 0, $i) . $rn . ($decimals
? $dec_point.(isset($b[1])
? str_replace('0.', '', sprintf('%0.'.$decimals.'f',
'0.'.$b[1]))
: str_repeat(0, $decimals))
: '');
}
/* Outputs 123 456,790 */
print my_number_format(123456.78987, 3);
?>
It can be optimized more, but this variant works well also.
----
Server IP: 69.147.83.197
Probable Submitter: 213.183.252.21
----
Manual Page -- http://www.php.net/manual/en/function.number-format.php
Edit -- https://master.php.net/note/edit/83438
Del: integrated -- https://master.php.net/note/delete/83438/integrated
Del: useless -- https://master.php.net/note/delete/83438/useless
Del: bad code -- https://master.php.net/note/delete/83438/bad+code
Del: spam -- https://master.php.net/note/delete/83438/spam
Del: non-english -- https://master.php.net/note/delete/83438/non-english
Del: in docs -- https://master.php.net/note/delete/83438/in+docs
Del: other reasons-- https://master.php.net/note/delete/83438
Reject -- https://master.php.net/note/reject/83438
Search -- https://master.php.net/manage/user-notes.php