note 40558 added to function.number-format

From: Date: Sun, 07 Mar 2004 22:29:58 +0000
Subject: note 40558 added to function.number-format
Groups: php.notes 
Request: Send a blank email to php-notes+get-66409@lists.php.net to get a copy of this message
People here in India are more used to counting money in Lakhs & Crores .. so here is the code for formatting the commas with thousands for the first time and then with hundred multiples from there after. Ex: 1234567 -> 12,34,567 <?php function makecomma($input) { // This function is written by some anonymous person - I got it from Google if(strlen($input)<=2) { return $input; } $length=substr($input,0,strlen($input)-2); $formatted_input = makecomma($length).",".substr($input,-2); return $formatted_input; } function formatInIndianStyle($num){ // This is my function $pos = strpos((string)$num, "."); if ($pos === false) { $decimalpart="00";} else { $decimalpart= substr($num, $pos+1, 2); $num = substr($num,0,$pos); } if(strlen($num)>3 & strlen($num) <= 12){ $last3digits = substr($num, -3 ); $numexceptlastdigits = substr($num, 0, -3 ); $formatted = makecomma($numexceptlastdigits); $stringtoreturn = $formatted.",".$last3digits.".".$decimalpart ; }elseif(strlen($num)<=3){ $stringtoreturn = $num.".".$decimalpart ; }elseif(strlen($num)>12){ $stringtoreturn = number_format($num, 2); } if(substr($stringtoreturn,0,2)=="-,"){$stringtoreturn = "-".substr($stringtoreturn,2 );} return $stringtoreturn; } $num = 1234567; echo formatInIndianStyle($num); ?> ---- Manual Page -- http://www.php.net/manual/en/function.number-format.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+40558 Delete -- http://master.php.net/manage/user-notes.php?action=delete+40558&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+40558&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#66409) next »