note 105961 modified in function.money-format by danbrown

From: Date: Wed, 28 Sep 2011 15:01:46 +0000
Subject: note 105961 modified in function.money-format by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-183506@lists.php.net to get a copy of this message
Hi, For South Asian Currencies, this function could be a handy one. It will handle negative as well as float(Paise). <?php function my_money_format($number) { if(strstr($number,"-")) { $number = str_replace("-","",$number); $negative = "-"; } $split_number = @explode(".",$number); $rupee = $split_number[0]; $paise = @$split_number[1]; if(@strlen($rupee)>3) { $hundreds = substr($rupee,strlen($rupee)-3); $thousands_in_reverse = strrev(substr($rupee,0,strlen($rupee)-3)); for($i=0; $i<(strlen($thousands_in_reverse)); $i=$i+2) { $thousands .= $thousands_in_reverse[$i].$thousands_in_reverse[$i+1].","; } $thousands = strrev(trim($thousands,",")); $formatted_rupee = $thousands.",".$hundreds; } else { $formatted_rupee = $rupee; } if((int)$paise>0) { $formatted_paise = ".".substr($paise,0,2); } return $negative.$formatted_rupee.$formatted_paise; } ?> Thanks, --was-- Hi, For South Asian Currencies, this function could be a handy one. It will handle negative as well as float(Paise). function my_money_format($number) { if(strstr($number,"-")) { $number = str_replace("-","",$number); $negative = "-"; } $split_number = @explode(".",$number); $rupee = $split_number[0]; $paise = @$split_number[1]; if(@strlen($rupee)>3) { $hundreds = substr($rupee,strlen($rupee)-3); $thousands_in_reverse = strrev(substr($rupee,0,strlen($rupee)-3)); for($i=0; $i<(strlen($thousands_in_reverse)); $i=$i+2) { $thousands .= $thousands_in_reverse[$i].$thousands_in_reverse[$i+1].","; } $thousands = strrev(trim($thousands,",")); $formatted_rupee = $thousands.",".$hundreds; } else { $formatted_rupee = $rupee; } if((int)$paise>0) { $formatted_paise = ".".substr($paise,0,2); } return $negative.$formatted_rupee.$formatted_paise; } Thanks, http://php.net/manual/en/function.money-format.php

« previous php.notes (#183506) next »