phpnumberscpu-word

Number to words using NumberFormatter class


I have a difficulty solving my number to words function in PHP.

$num = 29.29;
$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
echo $f->format($num);
//outputs Twenty-nine and two nine

How can I format that to: Twenty-nine and Twenty-nine?


Solution

  • First of all, how 29.29 should pronounce is Twenty nine point two nine. Having said that, if you need to get exactly Twenty-nine and Twenty-nine, you can use below :

    <?php
      $num = 29.29;
      $exp = explode('.', $num);
      $f = new NumberFormatter("en_US", NumberFormatter::SPELLOUT);
      echo ucfirst($f->format($exp[0])) . ' and ' . ucfirst($f->format($exp[1]));
      //outputs Twenty-nine and Twenty-nine
    ?>