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
<
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
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);
}
}
?>
In words :