lazyfox lazyfox - 1 year ago 106
PHP Question

Number to words in php using NumberFormatter class

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



<?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?

Please help!

PSJ PSJ
Answer Source

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
?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download