I just need a little help here. Because I am creating a code for converting decimals to Money format in words. For example if
I have this number
<
I tried to add something for the decimal value. Hope it will help.
<?php
function convertNumber($number)
{
list($integer,$fraction) = explode(".", (string) $number);
$output = " ";
if ($integer{0} == "-")
{
$output = "negative ";
$integer = ltrim($integer, "-");
}
else if ($integer{0} == "+")
{
$output = "positive ";
$integer = ltrim($integer, "+");
}
if ($integer{0} == "0")
{
$output .= "zero";
}
else
{
$integer = str_pad($integer, 36, "0", STR_PAD_LEFT);
$group = rtrim(chunk_split($integer, 3, " "), " ");
$groups = explode(" ", $group);
$groups2 = array();
foreach ($groups as $g)
{
$groups2[] = convertThreeDigit($g{0}, $g{1}, $g{2});
}
for ($z = 0; $z < count($groups2); $z++)
{
if ($groups2[$z] != "")
{
$output .= $groups2[$z] . convertGroup(11 - $z) . (
$z < 11
&& !array_search('', array_slice($groups2, $z + 1, -1))
&& $groups2[11] != ''
&& $groups[11]{0} == '0'
? " and "
: " "
);
}
}
$output = rtrim($output, ", ");
}
if ($fraction > 0)
{
$output .= " PESOS ";
for ($i = 0; $i < strlen($fraction); $i++)
{
if($fraction==01){
$i = 1;
while ($i <2):
$output .= " and one centavo only";
$i++;
endwhile;
}
else if($fraction==02){
$i = 1;
while ($i <2):
$output .= " and two centavos only";
$i++;
endwhile;
}
else if($fraction==03){
$i = 1;
while ($i <2):
$output .= " and three centavos only";
$i++;
endwhile;
}
else if($fraction==04){
$i = 1;
while ($i <2):
$output .= " and four centavos only";
$i++;
endwhile;
}
else if($fraction==05){
$i = 1;
while ($i <2):
$output .= " and five centavos only";
$i++;
endwhile;
}
else if($fraction==06){
$i = 1;
while ($i <2):
$output .= " and six centavos only";
$i++;
endwhile;
}
else if($fraction==07){
$i = 1;
while ($i <2):
$output .= " point seven centavos only";
$i++;
endwhile;
}
else if($fraction==8 || $fraction==08){
$i = 1;
while ($i <2):
$output .= " and eight centavos only";
$i++;
endwhile;
}
else if($fraction==9 || $fraction==09){
$i = 1;
while ($i <2):
$output .= " and nine centavos only";
$i++;
endwhile;
}
else if($fraction==10){
$i = 1;
while ($i <2):
$output .= " and ten centavos only";
$i++;
endwhile;
}
else if($fraction==11){
$i = 1;
while ($i <2):
$output .= " and eleven centavos only";
$i++;
endwhile;
}
else if($fraction==12){
$i = 1;
while ($i <2):
$output .= " and twelve centavos only";
$i++;
endwhile;
}
else if($fraction==13){
$i = 1;
while ($i <2):
$output .= " and thirteen centavos only";
$i++;
endwhile;
}
else if($fraction==14){
$i = 1;
while ($i <2):
$output .= " and fourteen centavos only";
$i++;
endwhile;
}
else if($fraction==15){
$i = 1;
while ($i <2):
$output .= " and fifteen centavos only";
$i++;
endwhile;
}
else if($fraction==16){
$i = 1;
while ($i <2):
$output .= " and sixteen centavos only";
$i++;
endwhile;
}
else if($fraction==17){
$i = 1;
while ($i <2):
$output .= " and seventeen centavos only";
$i++;
endwhile;
}
else if($fraction==18){
$i = 1;
while ($i <2):
$output .= " and eighteen centavos only";
$i++;
endwhile;
}
else if($fraction==19){
$i = 1;
while ($i <2):
$output .= " and nineteen centavos only";
$i++;
endwhile;
}
else if($fraction==20){
$i = 1;
while ($i <2):
$output .= " and twenty centavos only";
$i++;
endwhile;
}
else if($fraction==21){
$i = 1;
while ($i <2):
$output .= " and twenty one centavos only";
$i++;
endwhile;
}
else if($fraction==22){
$i = 1;
while ($i <2):
$output .= " and twenty two centavos only";
$i++;
endwhile;
}
else if($fraction==23){
$i = 1;
while ($i <2):
$output .= " and twenty three centavos only";
$i++;
endwhile;
}
else if($fraction==24){
$i = 1;
while ($i <2):
$output .= " and twenty four centavos only";
$i++;
endwhile;
}
else if($fraction==25){
$i = 1;
while ($i <2):
$output .= " and twenty five centavos only";
$i++;
endwhile;
}
else if($fraction==26){
$i = 1;
while ($i <2):
$output .= " and twenty six centavos only";
$i++;
endwhile;
}
else if($fraction==27){
$i = 1;
while ($i <2):
$output .= " and twenty seven centavos only";
$i++;
endwhile;
}
else if($fraction==28){
$i = 1;
while ($i <2):
$output .= " and twenty eight centavos only";
$i++;
endwhile;
}
else if($fraction==29){
$i = 1;
while ($i <2):
$output .= " and twenty nine centavos only";
$i++;
endwhile;
}
else if($fraction ==30){
$i = 1;
while ($i <2):
$output .= " and thirty centavos only";
$i++;
endwhile;
}
else if($fraction ==31){
$i = 1;
while ($i <2):
$output .= " and thirty one centavos only";
$i++;
endwhile;
}
else if($fraction ==32){
$i = 1;
while ($i <2):
$output .= " and thirty two centavos only";
$i++;
endwhile;
}
else if($fraction ==33){
$i = 1;
while ($i <2):
$output .= " and thirty three centavos only";
$i++;
endwhile;
}
else if($fraction ==34){
$i = 1;
while ($i <2):
$output .= " and thirty four centavos only";
$i++;
endwhile;
}
else if($fraction ==35){
$i = 1;
while ($i <2):
$output .= " and thirty five centavos only";
$i++;
endwhile;
}
else if($fraction ==36){
$i = 1;
while ($i <2):
$output .= " and thirty six centavos only";
$i++;
endwhile;
}
else if($fraction ==37){
$i = 1;
while ($i <2):
$output .= " and thirty seven centavos only";
$i++;
endwhile;
}
else if($fraction ==38){
$i = 1;
while ($i <2):
$output .= " and thirty eight centavos only";
$i++;
endwhile;
}
else if($fraction ==39){
$i = 1;
while ($i <2):
$output .= " and thirty nine centavos only";
$i++;
endwhile;
}
else if($fraction ==40){
$i = 1;
while ($i <2):
$output .= " and fourty centavos only";
$i++;
endwhile;
}
else if($fraction ==41){
$i = 1;
while ($i <2):
$output .= " and fourty one centavos only";
$i++;
endwhile;
}
else if($fraction ==42){
$i = 1;
while ($i <2):
$output .= " and fourty two centavos only";
$i++;
endwhile;
}
else if($fraction ==43){
$i = 1;
while ($i <2):
$output .= " and fourty three centavos only";
$i++;
endwhile;
}
else if($fraction ==44){
$i = 1;
while ($i <2):
$output .= " and fourty four centavos only";
$i++;
endwhile;
}
else if($fraction ==45){
$i = 1;
while ($i <2):
$output .= " and fourty five centavos only";
$i++;
endwhile;
}
else if($fraction ==46){
$i = 1;
while ($i <2):
$output .= " and fourty six centavos only";
$i++;
endwhile;
}
else if($fraction ==47){
$i = 1;
while ($i <2):
$output .= " and fourty seven centavos only";
$i++;
endwhile;
}
else if($fraction ==48){
$i = 1;
while ($i <2):
$output .= " and fourty eight centavos only";
$i++;
endwhile;
}
else if($fraction ==49){
$i = 1;
while ($i <2):
$output .= " and fourty nine centavos only";
$i++;
endwhile;
}
else if($fraction ==50){
$i = 1;
while ($i <2):
$output .= " and fifty centavos only";
$i++;
endwhile;
}
else if($fraction ==51){
$i = 1;
while ($i <2):
$output .= " and fifty one centavos only";
$i++;
endwhile;
}
else if($fraction ==52){
$i = 1;
while ($i <2):
$output .= " and fifty two centavos only";
$i++;
endwhile;
}
else if($fraction ==53){
$i = 1;
while ($i <2):
$output .= " and fifty three centavos only";
$i++;
endwhile;
}
else if($fraction ==54){
$i = 1;
while ($i <2):
$output .= " and fifty four centavos only";
$i++;
endwhile;
}
else if($fraction ==55){
$i = 1;
while ($i <2):
$output .= " and fifty five centavos only";
$i++;
endwhile;
}
else if($fraction ==56){
$i = 1;
while ($i <2):
$output .= " and fifty six centavos only";
$i++;
endwhile;
}
else if($fraction ==57){
$i = 1;
while ($i <2):
$output .= " and fifty seven centavos only";
$i++;
endwhile;
}
else if($fraction ==58){
$i = 1;
while ($i <2):
$output .= " and fifty eight centavos only";
$i++;
endwhile;
}
else if($fraction ==59){
$i = 1;
while ($i <2):
$output .= " and fifty nine centavos only";
$i++;
endwhile;
}
else if($fraction ==60){
$i = 1;
while ($i <2):
$output .= " and sixty centavos only";
$i++;
endwhile;
}
else if($fraction ==61){
$i = 1;
while ($i <2):
$output .= " and sixty one centavos only";
$i++;
endwhile;
}
else if($fraction ==62){
$i = 1;
while ($i <2):
$output .= " and sixty two centavos only";
$i++;
endwhile;
}
else if($fraction ==63){
$i = 1;
while ($i <2):
$output .= " and sixty three centavos only";
$i++;
endwhile;
}
else if($fraction ==64){
$i = 1;
while ($i <2):
$output .= " and sixty four centavos only";
$i++;
endwhile;
}
else if($fraction ==65){
$i = 1;
while ($i <2):
$output .= " and sixty five centavos only";
$i++;
endwhile;
}
else if($fraction ==66){
$i = 1;
while ($i <2):
$output .= " and sixty six centavos only";
$i++;
endwhile;
}
else if($fraction ==67){
$i = 1;
while ($i <2):
$output .= " and sixty seven centavos only";
$i++;
endwhile;
}
else if($fraction ==68){
$i = 1;
while ($i <2):
$output .= " and sixty eight centavos only";
$i++;
endwhile;
}
else if($fraction ==69){
$i = 1;
while ($i <2):
$output .= " and sixty nine centavos only";
$i++;
endwhile;
}
else if($fraction ==70){
$i = 1;
while ($i <2):
$output .= " and seventy centavos only";
$i++;
endwhile;
}
else if($fraction ==71){
$i = 1;
while ($i <2):
$output .= " and seventy one centavos only";
$i++;
endwhile;
}
else if($fraction ==72){
$i = 1;
while ($i <2):
$output .= " and seventy two centavos only";
$i++;
endwhile;
}
else if($fraction ==73){
$i = 1;
while ($i <2):
$output .= " and seventy three centavos only";
$i++;
endwhile;
}
else if($fraction ==74){
$i = 1;
while ($i <2):
$output .= " and seventy four centavos only";
$i++;
endwhile;
}
else if($fraction ==75){
$i = 1;
while ($i <2):
$output .= " and seventy five centavos only";
$i++;
endwhile;
}
else if($fraction ==76){
$i = 1;
while ($i <2):
$output .= " and seventy six centavos only";
$i++;
endwhile;
}
else if($fraction ==77){
$i = 1;
while ($i <2):
$output .= " and seventy seven centavos only";
$i++;
endwhile;
}
else if($fraction ==78){
$i = 1;
while ($i <2):
$output .= " and seventy eight centavos only";
$i++;
endwhile;
}
else if($fraction ==79){
$i = 1;
while ($i <2):
$output .= " and seventy nine centavos only";
$i++;
endwhile;
}
else if($fraction ==80){
$i = 1;
while ($i <2):
$output .= " and eighty centavos only";
$i++;
endwhile;
}
else if($fraction ==81){
$i = 1;
while ($i <2):
$output .= " and eighty one centavos only";
$i++;
endwhile;
}
else if($fraction ==82){
$i = 1;
while ($i <2):
$output .= " and eighty two centavos only";
$i++;
endwhile;
}
else if($fraction ==83){
$i = 1;
while ($i <2):
$output .= " and eighty three centavos only";
$i++;
endwhile;
}
else if($fraction ==84){
$i = 1;
while ($i <2):
$output .= " and eighty four centavos only";
$i++;
endwhile;
}
else if($fraction ==85){
$i = 1;
while ($i <2):
$output .= " and eighty five centavos only";
$i++;
endwhile;
}
else if($fraction ==86){
$i = 1;
while ($i <2):
$output .= " and eighty six centavos only";
$i++;
endwhile;
}
else if($fraction ==87){
$i = 1;
while ($i <2):
$output .= " and eighty seven centavos only";
$i++;
endwhile;
}
else if($fraction ==88){
$i = 1;
while ($i <2):
$output .= " and eighty eight centavos only";
$i++;
endwhile;
}
else if($fraction ==89){
$i = 1;
while ($i <2):
$output .= " and eighty nine centavos only";
$i++;
endwhile;
}
else if($fraction ==90){
$i = 1;
while ($i <2):
$output .= " and ninety centavos only";
$i++;
endwhile;
}
else if($fraction ==91){
$i = 1;
while ($i <2):
$output .= " and ninety one centavos only";
$i++;
endwhile;
}
else if($fraction ==92){
$i = 1;
while ($i <2):
$output .= " and ninety two centavos only";
$i++;
endwhile;
}
else if($fraction ==93){
$i = 1;
while ($i <2):
$output .= " and ninety three centavos only";
$i++;
endwhile;
}
else if($fraction ==94){
$i = 1;
while ($i <2):
$output .= " and ninety four centavos only";
$i++;
endwhile;
}
else if($fraction ==95){
$i = 1;
while ($i <2):
$output .= " and ninety five centavos only";
$i++;
endwhile;
}
else if($fraction ==96){
$i = 1;
while ($i <2):
$output .= " and ninety six centavos only";
$i++;
endwhile;
}
else if($fraction ==97){
$i = 1;
while ($i <2):
$output .= " and ninety seven centavos only";
$i++;
endwhile;
}
else if($fraction ==98){
$i = 1;
while ($i <2):
$output .= " and ninety eight centavos only";
$i++;
endwhile;
}
else if($fraction ==99){
$i = 1;
while ($i <2):
$output .= " and ninety nine centavos only";
$i++;
endwhile;
}
}
}
else{
$output .= " PESOS ONLY";
}
return $output;
}
function convertGroup($index)
{
switch ($index)
{
case 11:
return " decillion";
case 10:
return " nonillion";
case 9:
return " octillion";
case 8:
return " septillion";
case 7:
return " sextillion";
case 6:
return " quintrillion";
case 5:
return " quadrillion";
case 4:
return " trillion";
case 3:
return " billion";
case 2:
return " million";
case 1:
return " thousand";
case 0:
return "";
}
}
function convertThreeDigit($digit1, $digit2, $digit3)
{
$buffer = " ";
if ($digit1 == "0" && $digit2 == "0" && $digit3 == "0")
{
return "";
}
if ($digit1 != "0")
{
$buffer .= convertDigit($digit1) . " hundred";
if ($digit2 != "0" || $digit3 != "0")
{
$buffer .= " ";
}
}
if ($digit2 != "0")
{
$buffer .= convertTwoDigit($digit2, $digit3);
}
else if ($digit3 != "0")
{
$buffer .= convertDigit($digit3);
}
return $buffer;
}
function convertTwoDigit($digit1, $digit2)
{
if ($digit2 == "0")
{
switch ($digit1)
{
case "1":
return "ten";
case "2":
return "twenty";
case "3":
return "thirty";
case "4":
return "forty";
case "5":
return "fifty";
case "6":
return "sixty";
case "7":
return "seventy";
case "8":
return "eighty";
case "9":
return "ninety";
}
} else if ($digit1 == "1")
{
switch ($digit2)
{
case "1":
return "eleven";
case "2":
return "twelve";
case "3":
return "thirteen";
case "4":
return "fourteen";
case "5":
return "fifteen";
case "6":
return "sixteen";
case "7":
return "seventeen";
case "8":
return "eighteen";
case "9":
return "nineteen";
}
} else
{
$temp = convertDigit($digit2);
switch ($digit1)
{
case "2":
return "twenty $temp";
case "3":
return "thirty $temp";
case "4":
return "forty $temp";
case "5":
return "fifty $temp";
case "6":
return "sixty $temp";
case "7":
return "seventy $temp";
case "8":
return "eighty $temp";
case "9":
return "ninety $temp";
}
}
}
function convertDigit($digit)
{
switch ($digit)
{
case "0":
return "zero";
case "1":
return "one";
case "2":
return "two";
case "3":
return "three";
case "4":
return "four";
case "5":
return "five";
case "6":
return "six";
case "7":
return "seven";
case "8":
return "eight";
case "9":
return "nine";
}
}
strtoupper(convertNumber(2143.45)
?>
The function you are using is fine .
For
2143.45
I think the output should be :
two thousand one hundred forty three and four five cents
And not :
two thousand one hundred forty three and forty five cents
But if you would like it that way , you can still use the function you are using .
Definitely a longer way of achieving it !!! :
$testNumber = '2143.45';
$tempNum = explode( '.' , $testNumber );
$convertedNumber = ( isset( $tempNum[0] ) ? convertNumber( $tempNum[0] ) : '' );
// Use the below line if you don't want 'and' in the number before decimal point
$convertedNumber = str_replace( ' and ' ,' ' ,$convertedNumber );
// In the below line if you want you can replace ' and ' with ' , '
$convertedNumber .= ( ( isset( $tempNum[0] ) and isset( $tempNum[1] ) ) ? ' and ' : '' );
$convertedNumber .= ( isset( $tempNum[1] ) ? convertNumber( $tempNum[1] ) .' cents' : '' );
echo $convertedNumber;
Displays :
two thousand, one hundred forty-three and forty-five cents
You can also incorporate the above code lines in to your converNumber
function where it is translating the faction part if ($fraction > 0){ }
.
Money format (India - Rupee) (Working upto 13 digits)
Here i convert number into word format(Currency)
Ex : if you enter 1011, then you will get answer: One Thousand Eleven Rupee
Using string and array function and control structure(if)
// Ex: num = 1250.75
<?php
if (isset($_POST["num"])) {
// Getting number before(".")
$num = $_POST["num"]; // $num = 1250.75
$num_length = strcspn($num, "."); // length of $num
$new_num = substr($num, 0, $num_length); // $new_num = 1250
$new_num = (double) $new_num; // this will remove zero like 0001 = 1
$length = strlen($new_num); // length of $new_num
//Getting number after(".")
$num2 = strchr($num, "."); // $num2 = .75
$num2_length = strlen($num2); // length of $num2
$new_num2 = substr($num2, 1, 3); // This'll give you only '75' from '.75'
$new_num2 = (double) $new_num2; // remove zero like '05' equals to '5'
$precision_len = strlen($new_num2); // length of $new_num2
//Declare global variable
global $result; // to store result of 1250
global $result2; // to store result of 75
// Check length of input and call appropriate function
if ($length == 1) {
if ($new_num == 0 && $new_num2 == NULL) {
$result = "Zero Rupee";
} else {
$result = singleDigitWord($new_num) . " Rupee";
}
//echo $res;
} elseif ($length == 2) {
$result = twoDigitWord($new_num) . " Rupee";
//echo $res;
} elseif ($length == 3) {
$result = threeDigitWord($new_num) . " Rupee";
//echo $res;
} elseif ($length == 4 || $length == 5) {
$result = fourOrFiveDigitWord($length, $new_num) . " Rupee";
} elseif ($length == 6 || $length == 7) {
$result = sixOrSevenDigitWord($length, $new_num) . " Rupee";
} elseif ($length == 8 || $length == 9) {
$result = eightOrNineDigitWord($length, $new_num) . " Rupee";
} elseif ($length == 10 || $length == 11) {
$result = tenOrElevenDigitWord($length, $new_num) . " Rupee";
} elseif ($length == 12 || $length == 13) {
$result = twelveOrThirteenDigitWord($length, $new_num) . " Rupee";
}
if ($new_num2 > 0) {
if ($precision_len == 1) {
$result2 = singleDigitWord($new_num2) . " Paisa";
} elseif ($precision_len == 2) {
$result2 = twoDigitWord($new_num2) . " Paisa";
}
}
}
// Array containing value 1 to 9
function singleDigitWord($a)
{
$array = [
"1" => "One",
"2" => "Two",
"3" => "Three",
"4" => "Four",
"5" => "Five",
"6" => "Six",
"7" => "Seven",
"8" => "Eight",
"9" => "Nine",
];
if (array_key_exists((int) $a, $array)) {
return $array[$a];
}
}
// Array containing value 10 to 19
function ten2nineteen($a)
{
$array = [
"10" => "Ten",
"11" => "Eleven",
"12" => "Twelve",
"13" => "Thirteen",
"14" => "Fourteen",
"15" => "Fifteen",
"16" => "Sixteen",
"17" => "Seventeen",
"18" => "Eighteen",
"19" => "Nineteen",
];
if (array_key_exists((int) $a, $array)) {
return $array[$a];
}
}
// if length is 2,then 10 to 99 digits conversion
function twoDigitWord($a)
{
if ($a >= 10 && $a <= 19) {
$output = ten2nineteen($a);
return $output;
} elseif ($a >= 20 && $a <= 29) {
if ($a == 20) {
return "Twenty";
} else {
$a = $a - 20;
$y = singleDigitWord($a);
return "Twenty " . $y;
}
} elseif ($a >= 30 && $a <= 39) {
if ($a == 30) {
return "Thirty";
} else {
$a = $a - 30;
$y = singleDigitWord($a);
return "Thirty " . $y;
}
} elseif ($a >= 40 && $a <= 49) {
if ($a == 40) {
return "Forty";
} else {
$a = $a - 40;
$y = singleDigitWord($a);
return "Forty " . $y;
}
} elseif ($a >= 50 && $a <= 59) {
if ($a == 50) {
return "Fifty";
} else {
$a = $a - 50;
$y = singleDigitWord($a);
return "Fifty " . $y;
}
} elseif ($a >= 60 && $a <= 69) {
if ($a == 60) {
return "Sixty";
} else {
$a = $a - 60;
$y = singleDigitWord($a);
return "Sixty " . $y;
}
} elseif ($a >= 70 && $a <= 79) {
if ($a == 70) {
return "Seventy";
} else {
$a = $a - 70;
$y = singleDigitWord($a);
return "Seventy " . $y;
}
} elseif ($a >= 80 && $a <= 89) {
if ($a == 80) {
return "Eighty";
} else {
$a = $a - 80;
$y = singleDigitWord($a);
return "Eighty " . $y;
}
} elseif ($a >= 90 && $a <= 99) {
if ($a == 90) {
return "Ninty";
} else {
$a = $a - 90;
$y = singleDigitWord($a);
return "Ninty " . $y;
}
}
}
// if length is 3,then 100 to 999 digits conversion
function threeDigitWord($a)
{
$output = str_split($a);
$first = singleDigitWord($output[0]);
$lastdigits = substr($a, -2);
$lastdigits = (double) $lastdigits;
$second = lastDigitWord($lastdigits);
return $first . " Hundred " . $second;
}
// if length is 4 or 5,then 1000 to 99999 digits conversion
function fourOrFiveDigitWord($length, $a)
{
if ($length == 4) {
$output = str_split($a);
$first = singleDigitWord($output[0]);
} elseif ($length == 5) {
$output = str_split($a, 2);
$first = twoDigitWord($output[0]);
}
$lastdigits = substr($a, -3);
$lastdigits = (double) $lastdigits;
$second = lastDigitWord($lastdigits);
return $first . " Thousand " . $second;
}
// if length is 6 or 7,then 100000 to 9999999 digits conversion
function sixOrSevenDigitWord($length, $a)
{
if ($length == 6) {
$output = str_split($a);
$first = singleDigitWord($output[0]);
} elseif ($length == 7) {
$output = str_split($a, 2);
$first = twoDigitWord($output[0]);
}
$lastdigits = substr($a, -5);
$lastdigits = (double) $lastdigits;
$second = lastDigitWord($lastdigits);
return $first . " Lakh " . $second;
}
// if length is 8 or 9,then 10000000 to 999999999 digits conversion
function eightOrNineDigitWord($length, $a)
{
if ($length == 8) {
$output = str_split($a);
$first = singleDigitWord($output[0]);
} elseif ($length == 9) {
$output = str_split($a, 2);
$first = twoDigitWord($output[0]);
}
$lastdigits = substr($a, -7);
$lastdigits = (double) $lastdigits;
$second = lastDigitWord($lastdigits);
return $first . " Crore " . $second;
}
// if length is 10 or 11,then 1000000000 to 99999999999 digits conversion
function tenOrElevenDigitWord($length, $a)
{
if ($length == 10) {
$output = str_split($a);
$first = singleDigitWord($output[0]);
} elseif ($length == 11) {
$output = str_split($a, 2);
$first = twoDigitWord($output[0]);
}
$lastdigits = substr($a, -9);
$lastdigits = (double) $lastdigits;
$second = lastDigitWord($lastdigits);
return $first . " Billion " . $second;
}
// if length is 12 or 13,then 100000000000 to 99999999999999 digits conversion
function twelveOrThirteenDigitWord($length, $a)
{
if ($length == 12) {
$output = str_split($a);
$first = singleDigitWord($output[0]);
} elseif ($length == 13) {
$output = str_split($a, 2);
$first = twoDigitWord($output[0]);
}
$lastdigits = substr($a, -11);
$lastdigits = (double) $lastdigits;
$second = lastDigitWord($lastdigits);
return $first . " Trillion " . $second;
}
// this function gets value of $lastdigits which is declared in function above
// like twelveOrThirteenDigitWord or many other
// Ex: if your num is 1111 then your last digit will be 111 and you'll get
// answer by calling this function. lastDigitWords function call one of above
// function but i declare it here beacause we do not need to write it in every
// function above to get conversion of $lastdigits
function lastDigitWord($a)
{
if ($a <= 9) {
return singleDigitWord($a);
} elseif ($a >= 10 && $a <= 99) {
return twoDigitWord($a);
} elseif ($a >= 100 && $a <= 999) {
return threeDigitWord($a);
} elseif ($a >= 1000 && $a <= 99999) {
$new_length = strlen($a);
return fourORFiveDigitWord($new_length, $a);
} elseif ($a >= 100000 && $a <= 9999999) {
$new_length = strlen($a);
return sixORSevenDigitWord($new_length, $a);
} elseif ($a >= 10000000 && $a <= 999999999) {
$new_length = strlen($a);
return eightOrNineDigitWord($new_length, $a);
} elseif ($a >= 1000000000 && $a <= 99999999999) {
$new_length = strlen($a);
return tenOrElevenDigitWord($new_length, $a);
}
}
?>
<html>
<body>
<form name="myForm" method="POST" action="">
<table cellspacing="10" cellpadding="10">
<tr>
<td><label for="num">Enter Number: </label></td>
<td><input type="text" name="num" id="num" value="<?php
if (isset($_POST['num'])) {
echo $_POST['num'];
}
?>"></td>
<td><input type="submit" name="submit" id="submit" value="submit"></td>
</tr>
</table>
</form>
<p><b>In words : </b>
<?php
global $result;
global $result2;
echo $result . " " . $result2;
?>
</p>
</body>
</html>
<?php function numtowords($num){
$decones = array(
'01' => "One",
'02' => "Two",
'03' => "Three",
'04' => "Four",
'05' => "Five",
'06' => "Six",
'07' => "Seven",
'08' => "Eight",
'09' => "Nine",
10 => "Ten",
11 => "Eleven",
12 => "Twelve",
13 => "Thirteen",
14 => "Fourteen",
15 => "Fifteen",
16 => "Sixteen",
17 => "Seventeen",
18 => "Eighteen",
19 => "Nineteen"
);
$ones = array(
0 => " ",
1 => "One",
2 => "Two",
3 => "Three",
4 => "Four",
5 => "Five",
6 => "Six",
7 => "Seven",
8 => "Eight",
9 => "Nine",
10 => "Ten",
11 => "Eleven",
12 => "Twelve",
13 => "Thirteen",
14 => "Fourteen",
15 => "Fifteen",
16 => "Sixteen",
17 => "Seventeen",
18 => "Eighteen",
19 => "Nineteen"
);
$tens = array(
0 => "",
2 => "Twenty",
3 => "Thirty",
4 => "Forty",
5 => "Fifty",
6 => "Sixty",
7 => "Seventy",
8 => "Eighty",
9 => "Ninety"
);
$hundreds = array(
"Hundred",
"Thousand",
"Million",
"Billion",
"Trillion",
"Quadrillion"
); //limit t quadrillion
$num = number_format($num,2,".",",");
$num_arr = explode(".",$num);
$wholenum = $num_arr[0];
$decnum = $num_arr[1];
$whole_arr = array_reverse(explode(",",$wholenum));
krsort($whole_arr);
$rettxt = "";
foreach($whole_arr as $key => $i){
if($i < 20){
$rettxt .= $ones[$i];
}
elseif($i < 100){
$rettxt .= $tens[substr($i,0,1)];
$rettxt .= " ".$ones[substr($i,1,1)];
}
else{
$rettxt .= $ones[substr($i,0,1)]." ".$hundreds[0];
$rettxt .= " ".$tens[substr($i,1,1)];
$rettxt .= " ".$ones[substr($i,2,1)];
}
if($key > 0){
$rettxt .= " ".$hundreds[$key]." ";
}
}
$rettxt = $rettxt." peso/s";
if($decnum > 0){
$rettxt .= " and ";
if($decnum < 20){
$rettxt .= $decones[$decnum];
}
elseif($decnum < 100){
$rettxt .= $tens[substr($decnum,0,1)];
$rettxt .= " ".$ones[substr($decnum,1,1)];
}
$rettxt = $rettxt." centavo/s";
}
return $rettxt;} ?>
you can use this by
echo numtowords(156.50);
output is
One Hundred Fifty Six peso/s and Fifty centavo/s only.
Credit to the owner: Alex Culango PS: i've edited some lines because it has some error when the tens have "0" and also for proper decimal conversion to words and the peso and centavo placement.
indian version
function convert_number_to_words($number) {
$hyphen = '-';
$conjunction = ' and ';
$separator = ', ';
$negative = 'negative ';
$decimal = ' point ';
$dictionary = array(
0 => 'zero',
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => 'five',
6 => 'six',
7 => 'seven',
8 => 'eight',
9 => 'nine',
10 => 'ten',
11 => 'eleven',
12 => 'twelve',
13 => 'thirteen',
14 => 'fourteen',
15 => 'fifteen',
16 => 'sixteen',
17 => 'seventeen',
18 => 'eighteen',
19 => 'nineteen',
20 => 'twenty',
30 => 'thirty',
40 => 'fourty',
50 => 'fifty',
60 => 'sixty',
70 => 'seventy',
80 => 'eighty',
90 => 'ninety',
100 => 'hundred',
1000 => 'thousand',
100000 => 'lakh',
10000000 => 'crore'
);
if (!is_numeric($number)) {
return false;
}
if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
// overflow
trigger_error(
'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
E_USER_WARNING
);
return false;
}
if ($number < 0) {
return $negative . $this->convert_number_to_words(abs($number));
}
$string = $fraction = null;
if (strpos($number, '.') !== false) {
list($number, $fraction) = explode('.', $number);
}
switch (true) {
case $number < 21:
$string = $dictionary[$number];
break;
case $number < 100:
$tens = ((int) ($number / 10)) * 10;
$units = $number % 10;
$string = $dictionary[$tens];
if ($units) {
$string .= $hyphen . $dictionary[$units];
}
break;
case $number < 1000:
$hundreds = $number / 100;
$remainder = $number % 100;
$string = $dictionary[$hundreds] . ' ' . $dictionary[100];
if ($remainder) {
$string .= $conjunction . $this->convert_number_to_words($remainder);
}
break;
case $number < 100000:
$thousands = ((int) ($number / 1000));
$remainder = $number % 1000;
$thousands = $this->convert_number_to_words($thousands);
$string .= $thousands . ' ' . $dictionary[1000];
if ($remainder) {
$string .= $separator . $this->convert_number_to_words($remainder);
}
break;
case $number < 10000000:
$lakhs = ((int) ($number / 100000));
$remainder = $number % 100000;
$lakhs = $this->convert_number_to_words($lakhs);
$string = $lakhs . ' ' . $dictionary[100000];
if ($remainder) {
$string .= $separator . $this->convert_number_to_words($remainder);
}
break;
case $number < 1000000000:
$crores = ((int) ($number / 10000000));
$remainder = $number % 10000000;
$crores = $this->convert_number_to_words($crores);
$string = $crores . ' ' . $dictionary[10000000];
if ($remainder) {
$string .= $separator . $this->convert_number_to_words($remainder);
}
break;
default:
$baseUnit = pow(1000, floor(log($number, 1000)));
$numBaseUnits = (int) ($number / $baseUnit);
$remainder = $number % $baseUnit;
$string = $this->convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
if ($remainder) {
$string .= $remainder < 100 ? $conjunction : $separator;
$string .= $this->convert_number_to_words($remainder);
}
break;
}
if (null !== $fraction && is_numeric($fraction)) {
$string .= $decimal;
$words = array();
foreach (str_split((string) $fraction) as $number) {
$words[] = $dictionary[$number];
}
$string .= implode(' ', $words);
}
return $string;
}
If you don't want to edit the function, a quick and dirty way you can do this is by hacking the parts where it prints the words, like this: http://pastebin.com/UwSR8NpV It should work like the way you wanted it to.