1

I have a problem in outputing words from eleven to nineteen, instead of eleven it outputs ten one and twelve outputs ten two and so on upto nineteen, but in decimals it correctly outputs ten to nineteen

PHP latest, Webserver

<?php
function toWords($num) {

  $ones = 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",
    "014" => "Fourteen"
  );
  $tens = array(
    0 => "Zero",
    1 => "Ten",
    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, 1);
  $rettxt = "";
  foreach ($whole_arr as $key => $i) {

    while (substr($i, 0, 1) == "0")
      $i = substr($i, 1, 5);
    if ($i < 20) {
      /* echo "getting:".$i; */
      $rettxt .= $ones[$i];
    } elseif ($i < 100) {
      if (substr($i, 0, 1) != "0")  $rettxt .= $tens[substr($i, 0, 1)];
      if (substr($i, 1, 1) != "0") $rettxt .= " ".$ones[substr($i, 1, 1)];
    } else {
      if (substr($i, 0, 1) != "0") $rettxt .= $ones[substr($i, 0, 1)]." ".$hundreds[0];
      if (substr($i, 1, 1) != "0")$rettxt .= " ".$tens[substr($i, 1, 1)];
      if (substr($i, 2, 1) != "0")$rettxt .= " ".$ones[substr($i, 2, 1)];
    }
    if ($key > 0) {
      $rettxt .= " ".$hundreds[$key]." ";
    }
  } $rettxt = $rettxt." Pesos";
  if ($decnum > 0) {
    $rettxt .= " and ";
    if ($decnum < 20) {
      $rettxt .= $ones[$decnum];
    } elseif ($decnum < 100) {
      $rettxt .= $tens[substr($decnum, 0, 1)];
      $rettxt .= " ".$ones[substr($decnum, 1, 1)];
    }

    $rettxt = $rettxt." Centavos";
  }
  return $rettxt;
}
?>

The output is Ten Two instead of Twelve (eleven to nineteen) But in decimal it output correctly the words eleven to nineteen

Four Thousand Two Hundred Ten Two Pesos (P 4,212.00), One Thousand Nine Hundred Ten Seven Pesos and Eighteen Centavos (P 1,917.18)... I fount this code at this website : https://www.studentstutorial.com/php/number-to-words.php

SMCT - IT
  • 11
  • 3
  • `echo toWords(11);` for me echos `Eleven Pesos` – Second2None Feb 19 '19 at 09:24
  • @Second2None try to echo with hundreds echo toWords(111); it outputs One Hundred Ten One Pesos – SMCT - IT Feb 19 '19 at 09:47
  • try this https://stackoverflow.com/questions/277569/is-there-an-easy-way-to-convert-a-number-to-a-word-in-php – Ekown Feb 19 '19 at 09:57
  • You need to do things in the right order - check the special cases (eleven, twelve, …) first, and only if you did not encounter one of those values, you proceed with the “normal” stuff. – 04FS Feb 19 '19 at 10:21
  • @04FS i just copied the code from the website, do you have any idea where and what part of the code was the error ? – SMCT - IT Feb 20 '19 at 00:44
  • **Do not follow studentstutorial.com tutorials!** They are advocating bad programming practices and their code samples are full of security problems. – Dharman Jun 21 '19 at 17:31

1 Answers1

0
<?php

$number = '11545';
$locale = 'en_US';
$format = numfmt_create($locale, NumberFormatter::SPELLOUT);
$in_words = numfmt_format($format, $number);

print_r($in_words);
// eleven thousand five hundred forty-five

?>
  • one hundred fifty thousand point five - 150,000.50 result one hundred fifty thousand and fifty - 150,000 expected – SMCT - IT Feb 20 '19 at 00:25