function updated to support decimal places
<?php
$nwords = array( "", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
"fourteen", "fifteen", "sixteen", "seventeen", "eightteen",
"nineteen", "twenty", 30 => "thirty", 40 => "fourty",
50 => "fifty", 60 => "sixty", 70 => "seventy", 80 => "eigthy",
90 => "ninety" );
function decimal_to_words($x)
{
$x = str_replace(',','',$x);
$pos = strpos((string)$x, ".");
if ($pos !== false) { $decimalpart= substr($x, $pos+1, 2); $x = substr($x,0,$pos); }
$tmp_str_rtn = number_to_words ($x);
if(!empty($decimalpart))
$tmp_str_rtn .= ' and ' . number_to_words ($decimalpart) . ' paise';
return $tmp_str_rtn;
}
function number_to_words ($x)
{
global $nwords;
if(!is_numeric($x))
{
$w = '#';
}else if(fmod($x, 1) != 0)
{
$w = '#';
}else{
if($x < 0)
{
$w = 'minus ';
$x = -$x;
}else{
$w = '';
}
if($x < 21)
{
$w .= $nwords[$x];
}else if($x < 100)
{
$w .= $nwords[10 * floor($x/10)];
$r = fmod($x, 10);
if($r > 0)
{
$w .= ' '. $nwords[$r];
}
} else if($x < 1000)
{
$w .= $nwords[floor($x/100)] .' hundred';
$r = fmod($x, 100);
if($r > 0)
{
$w .= ' '. number_to_words($r);
}
} else if($x < 100000)
{
$w .= number_to_words(floor($x/1000)) .' thousand';
$r = fmod($x, 1000);
if($r > 0)
{
$w .= ' ';
if($r < 100)
{
$w .= ' ';
}
$w .= number_to_words($r);
}
} else {
$w .= number_to_words(floor($x/100000)) .' lacs';
$r = fmod($x, 100000);
if($r > 0)
{
$w .= ' ';
if($r < 100)
{
$word .= ' ';
}
$w .= number_to_words($r);
}
}
}
return $w;
}
// demonstration
if(isset($_POST['num']))
{
echo '
'.htmlspecialchars($_POST['num']).' = '.decimal_to_words($_POST['num']).'<p>
<a href="'.$_SERVER['PHP_SELF'].'">try again</a>';
}else{
echo '
<form method="post" action="'.$_SERVER['PHP_SELF'].'">
<input type="text" name="num">
<input type="submit" value="spell number">
</form>';
}
?>
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104:





by: babuno5Posted on 2009-10-30 at 20:59:04ID: 25708348
Got it working for this 10,45,346
Will try to patch it for decimal places
Select allOpen in new window