note 68783 deleted from function.number-format by cmb

From: Date: Tue, 13 Nov 2018 11:37:57 +0000
Subject: note 68783 deleted from function.number-format by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-209689@lists.php.net to get a copy of this message
Note Submitter: j-a-n at gmx dot de ---- This function formats an decimal number to a String. But it does'n use an fixed count of decimals but calculates the needed count of decimals. <? function formatNumber( $number, $decimals=2, $dec_point=".", $thousands_sep=",") { $nachkomma = abs($in - floor($in)); $strnachkomma = number_format($nachkomma , $decimals, ".", ""); for ($i = 1; $i <= $decimals; $i++) { if (substr($strnachkomma, ($i * -1), 1) != "0") { break; } } return number_format($in, ($decimals - $i +1), $dec_point, $thousands_sep); } ?> Example: formatNumber( 100.00 ); --> 100 formatNumber( 100.50 ); --> 100.5 formatNumber( 100.1234 ); --> 100.12 formatNumber( 100.12, 4 ); --> 100.12 formatNumber( 100.12345, 4 ); --> 100.1234

« previous php.notes (#209689) next »