Last active
August 29, 2015 14:17
-
-
Save samratmazumder91/204a4839b518d2c5bee1 to your computer and use it in GitHub Desktop.
Number to Word Conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function n2w($a){ | |
$word = array(); | |
$deno = array(1000=>'hundred',100000=>'thousand',10000000=>'lac',1000000000=>'crore'); | |
$ones = array(1 => 'one',2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', 6=>'six',7=>'seven',8=>'eight',9=>'nine'); | |
$tens = array(1 => 'ten',2 => 'twenty', 3 => 'thirty', 4 => 'forty', 5 => 'fifty', 6=>'sixty',7=>'seventy',8=>'eighty',9=>'ninety'); | |
$exception = array(1 => 'eleven',2 => 'twelve', 3 => 'thirteen', 4 => 'fourteen', 5 => 'fiveteen', 6=>'sixteen',7=>'seventeen',8=>'eighteen',9=>'nineteen'); | |
$rup = explode('.',$a); | |
$whole = (int)preg_replace('/,/','',$rup[0]); | |
$t_place = $whole%100; | |
if($t_place > 9){ | |
$p = strval($t_place); | |
if((int)$p[0] == 1){ | |
$word[] = $exception[(int)$p[1]]; | |
} | |
else{ | |
$word[] = $ones[(int)$p[1]]; | |
$word[] = $tens[(int)$p[0]]; | |
} | |
} | |
else{ | |
$word[] = $ones[$t_place]; | |
} | |
//to get the hundreth place | |
if(($p = $whole % 1000) >= 100){ | |
$q = strval($p); | |
$word[] = $deno[1000]; | |
$word[] = $ones[(int)$q[0]]; | |
} | |
$me = 100000; | |
$flag = 0; | |
while($flag != 2){ | |
if($whole % $me == $whole){ | |
$flag++; | |
} | |
if(($p = $whole % $me) >= ($me/100)){ | |
$q = strval($p); | |
$word[] = $deno[$me]; | |
if($p >= $me/10){ | |
if($q[1] == '0'){ | |
$word[] = $tens[(int)$q[0]]; | |
} | |
else if($q[0] == '1' && $q[1] != '0'){ | |
$word[] = $exception[(int)$q[1]]; | |
} | |
else{ | |
$word[] = $ones[(int)$q[1]]; | |
$word[] = $tens[(int)$q[0]]; | |
} | |
} | |
else{ | |
$word[] = $ones[(int)$q[0]]; | |
} | |
} | |
$me *= 100; | |
} | |
echo implode( array_reverse( $word ), ' ' ) ; | |
} | |
n2w(234234324); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not working for numbers, which are greater than a million!