note 84420 added to function.number-format

From: Date: Sun, 13 Jul 2008 21:35:09 +0000
Subject: note 84420 added to function.number-format
Groups: php.notes 
Request: Send a blank email to php-notes+get-141458@lists.php.net to get a copy of this message
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; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 87.165.219.29 ---- Manual Page -- http://www.php.net/manual/en/function.number-format.php Edit -- https://master.php.net/note/edit/84420 Del: integrated -- https://master.php.net/note/delete/84420/integrated Del: useless -- https://master.php.net/note/delete/84420/useless Del: bad code -- https://master.php.net/note/delete/84420/bad+code Del: spam -- https://master.php.net/note/delete/84420/spam Del: non-english -- https://master.php.net/note/delete/84420/non-english Del: in docs -- https://master.php.net/note/delete/84420/in+docs Del: other reasons-- https://master.php.net/note/delete/84420 Reject -- https://master.php.net/note/reject/84420 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#141458) next »