note 113543 added to function.number-format
| From: | mobi dot lenoe at gmail dot com | Date: | Mon, 28 Oct 2013 08:13:41 +0000 |
| Subject: | note 113543 added to function.number-format | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-197336@lists.php.net to get a copy of this message | ||
I'd like to comment to the old notes of "stm555" and "woodynadobhar".
They wrote about "number_format_unlimited_precision()".
I guess many of us need that kind of function, which is the almost same function as number_format
but don't round a number.
Does Anyone know any new solution in a recent PHP version?
...
If no, how about the following function? (I fixed somethings like bugs of the function in the old
comment.)
<?php
function number_format_unchanged_precision($number, $dec_point='.',
$thousands_sep=','){
if($dec_point==$thousands_sep){
trigger_error('2 parameters for ' . __METHOD__ . '() have the same value, that is
"' . $dec_point . '" for $dec_point and $thousands_sep', E_USER_WARNING);
// It corresponds "PHP Warning: Wrong parameter count for number_format()", which
occurs when you use $dec_point without $thousands_sep to number_format().
}
if(preg_match('{\.\d+}', $number, $matches)===1){
$decimals = strlen($matches[0]) - 1;
}else{
$decimals = 0;
}
return number_format($number, $decimals, $dec_point, $thousands_sep);
}
var_dump(number_format_unchanged_precision(1234.5678, ',', '.'));
var_dump(number_format_unchanged_precision(1234.5678, ','));
var_dump(number_format_unchanged_precision(12345678));
var_dump(number_format_unchanged_precision(-0.5678, ',', '.')); // It occurred
a bug with the function in the old comment.
?>
output is:
string(10) "1.234,5678"
PHP Warning: 2 parameters for number_format_unchanged_precision() have the same value, that is
"," for $dec_point and $thousands_sep in...
string(10) "1,234,5678"
string(10) "12,345,678"
string(7) "-0,5678"
----
Server IP: 192.41.45.150
Probable Submitter: 59.190.191.150
----
Manual Page -- http://php.net/manual/en/function.number-format.php
Edit -- https://master.php.net/note/edit/113543
Del: integrated -- https://master.php.net/note/delete/113543/integrated
Del: useless -- https://master.php.net/note/delete/113543/useless
Del: bad code -- https://master.php.net/note/delete/113543/bad+code
Del: spam -- https://master.php.net/note/delete/113543/spam
Del: non-english -- https://master.php.net/note/delete/113543/non-english
Del: in docs -- https://master.php.net/note/delete/113543/in+docs
Del: other reasons-- https://master.php.net/note/delete/113543
Reject -- https://master.php.net/note/reject/113543
Search -- https://master.php.net/manage/user-notes.php