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
'2143.45'
the output should be
'two thousand one hundred forty three and forty-five cents'
I found a code like this but I don't have an idea how to include cents.
<?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 .= " point";
for ($i = 0; $i < strlen($fraction); $i++)
{
$output .= " " . convertDigit($fraction{$i});
}
}
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 .= " and ";
}
}
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";
}
}
$num = 500254.89;
$test = convertNumber($num);
echo $test;
?>
php class numbertowordconvertsconver { function convert_number($number) { if (($number < 0) || ($number > 999999999)) { throw new Exception("Number is out of range"); } $giga = floor($number / 1000000); // Millions (giga) $number -= $giga * 1000000; $kilo = floor($number / 1000); // Thousands (kilo) $number -= $kilo * ...
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;
}
I don't know about cents but I have created code for Indian Rupees.
<?php
echo getIndianCurrency(2851.05);
function getIndianCurrency(float $number)
{
$decimal = round($number - ($no = floor($number)), 2) * 100;
$decimal_part = $decimal;
$hundred = null;
$hundreds = null;
$digits_length = strlen($no);
$decimal_length = strlen($decimal);
$i = 0;
$str = array();
$str2 = array();
$words = 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', 20 => 'twenty', 30 => 'thirty',
40 => 'forty', 50 => 'fifty', 60 => 'sixty',
70 => 'seventy', 80 => 'eighty', 90 => 'ninety');
$digits = array('', 'hundred','thousand','lakh', 'crore');
while( $i < $digits_length ) {
$divider = ($i == 2) ? 10 : 100;
$number = floor($no % $divider);
$no = floor($no / $divider);
$i += $divider == 10 ? 1 : 2;
if ($number) {
$plural = (($counter = count($str)) && $number > 9) ? 's' : null;
$hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
$str [] = ($number < 21) ? $words[$number].' '. $digits[$counter]. $plural.' '.$hundred:$words[floor($number / 10) * 10].' '.$words[$number % 10]. ' '.$digits[$counter].$plural.' '.$hundred;
} else $str[] = null;
}
$d = 0;
while( $d < $decimal_length ) {
$divider = ($d == 2) ? 10 : 100;
$decimal_number = floor($decimal % $divider);
$decimal = floor($decimal / $divider);
$d += $divider == 10 ? 1 : 2;
if ($decimal_number) {
$plurals = (($counter = count($str2)) && $decimal_number > 9) ? 's' : null;
$hundreds = ($counter == 1 && $str2[0]) ? ' and ' : null;
@$str2 [] = ($decimal_number < 21) ? $words[$decimal_number].' '. $digits[$decimal_number]. $plural.' '.$hundred:$words[floor($decimal_number / 10) * 10].' '.$words[$decimal_number % 10]. ' '.$digits[$counter].$plural.' '.$hundred;
} else $str2[] = null;
}
$Rupees = implode('', array_reverse($str));
$paise = implode('', array_reverse($str2));
$paise = ($decimal_part > 0) ? $paise . ' Paise' : '';
return ($Rupees ? $Rupees . 'Rupees ' : '') . $paise;
}
?>
Output:
two thousand eight hundred and fifty one Rupees five Paise
I will write a post about its code description on guptatreepoint.com
<?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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With