note 84420 deleted from function.number-format by danbrown

From: Date: Mon, 25 May 2009 22:20:59 +0000
Subject: note 84420 deleted from function.number-format by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156001@lists.php.net to get a copy of this message
Note Submitter: info at daniel-marschall dot de ---- Let's say we got the number $inp = 1234.56 By using <?php return number_format($inp, 2, ',', '.'); ?> you can get the German format 1.234,56. (Comma as decimal separator and point as thousand separator) But I have a problem with that: I want to add commas as thousand separators and change the decimal-separator (this could also be done with str_replace), but I do not want to change the amount of fractional digits! But since the 2nd argument of number_format is necessary to enter the 3rd and 4th argument, this cannot be done with number_format. You have to change the fractional digits with this function. But I want that 1234.56 changes into 1.234,56 and 1234.567890123456 changes into 1.234,567890123456 So, I created following function, that doesn't change the amount of fractional digits: <?php function my_number_format($number, $dec_point, $thousands_sep) { $tmp = explode('.', $number); $out = number_format($tmp[0], 0, $dec_point, $thousands_sep); if (isset($tmp[1])) $out .= $dec_point.$tmp[1]; return $out; } ?>

« previous php.notes (#156001) next »