jeeva Posted January 3, 2007 Share Posted January 3, 2007 hi frndsi hope u can help meWill u please tell me about how to convert the numbers to word in php or someother language like javascript...For exampleif i have given [b]1,60,82,000 [/b] it has to be converted to "[b]One crorer Sixty lakhs Eighty two thousand[/b]" only.Rgrds and ThanksJeeva Quote Link to comment Share on other sites More sharing options...
taith Posted January 3, 2007 Share Posted January 3, 2007 <?php function numtostr($number){ $number = strval($number); if(!ereg("^[0-9]{1,15}$", $number)) return false; $ones = array("", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"); $teens = array("ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"); $tens = array("", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"); $majorUnits = array("", "thousand", "million", "billion", "trillion"); $result = ""; $isAnyMajorUnit = false; $length = strlen($number); for($i=0, $pos=$length-1; $i<$length; $i++, $pos--){ if($number{$i} != '0'){ if($pos % 3 == 0) $result .= $ones[$number{$i}] . ' '; elseif($pos % 3 == 1){ if($number{$i} == '1'){ $result .= $teens[$number{$i + 1}] . ' '; $i++; $pos--; }else{ $result .= $tens[$number{$i}]; $result .= $number{$i + 1} == '0'? ' ' : '-'; } }else $result .= $ones[$number{$i}] . " hundred "; $isAnyMajorUnit = true; } if($pos % 3 == 0 && $isAnyMajorUnit){ $result .= $majorUnits[$pos / 3] . ' '; $isAnyMajorUnit = false; } } trim($result); if($result == "") $result = "zero"; return $result;}?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.