note 85052 added to function.money-format

From: Date: Tue, 12 Aug 2008 23:00:55 +0000
Subject: note 85052 added to function.money-format
Groups: php.notes 
Request: Send a blank email to php-notes+get-143425@lists.php.net to get a copy of this message
@ swapnet (22-Jul-2008 08:24) Your code will fail to convert 1000000. It will convert to 10,0,000. Only the first token from the split should be int typecasted, as this might have a leading zero. Here is the fixed version: <?php function convertcash($num, $currency){ if(strlen($num)>3){ $lastthree = substr($num, strlen($num)-3, strlen($num)); $restunits = substr($num, 0, strlen($num)-3); // extracts the last three digits $restunits = (strlen($restunits)%2 == 1)?"0".$restunits:$restunits; // explodes the remaining digits in 2's formats, adds a zero in the beginning to maintain the 2's grouping. $expunit = str_split($restunits, 2); if(sizeof($expunit) >= 1) { $explrestunits .= (int)$expunit[0].","; // int typecaste to get rid of the possible leading zero. } for($i=1; $i<sizeof($expunit); $i++){ $explrestunits .= $expunit[$i].","; // creates each of the 2's group and adds a comma to the end } $thecash = $explrestunits.$lastthree; } else { $thecash = $convertnum; } return $currency.$thecash.".00"; // writes the final format where $currency is the currency symbol. } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 67.180.236.15 ---- Manual Page -- http://www.php.net/manual/en/function.money-format.php Edit -- https://master.php.net/note/edit/85052 Del: integrated -- https://master.php.net/note/delete/85052/integrated Del: useless -- https://master.php.net/note/delete/85052/useless Del: bad code -- https://master.php.net/note/delete/85052/bad+code Del: spam -- https://master.php.net/note/delete/85052/spam Del: non-english -- https://master.php.net/note/delete/85052/non-english Del: in docs -- https://master.php.net/note/delete/85052/in+docs Del: other reasons-- https://master.php.net/note/delete/85052 Reject -- https://master.php.net/note/reject/85052 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#143425) next »