note 111397 deleted from function.number-format by cmb
| From: | cmb@php.net | Date: | Tue, 13 Nov 2018 11:41:18 +0000 |
| Subject: | note 111397 deleted from function.number-format by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-209695@lists.php.net to get a copy of this message | ||
Note Submitter: rlerne at gmail dot com
----
I came into a need to show two decimal places, but the desire to drop the decimal portion off if it
is zero. This function is a direct replacement for number_format:
<?PHP
function
number_format_clean($number,$precision=0,$dec_point='.',$thousands_sep=',')
{
RETURN trim(number_format($number,$precision,$dec_point,$thousands_sep),'0'.$dec_point);
}
?>
Example:
<?PHP
echo number_format_clean(25.00,2);
echo number_format_clean(25.52,2);
echo number_format_clean(50.10,2);
echo number_format_clean(05.50,2);
?>
Outputs:
25
25.52
50.1
5.5
Good for weight conversions (1 oz vs. 1.00 oz). Feels less "automated."
The above will trim all insignificant zeros from both ends of the string, though I'm not sure
I've ever seen leading zeros returned.