Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number to words in php using NumberFormatter class

Tags:

php

numbers

words

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!

like image 929
lazyfox Avatar asked Dec 11 '22 09:12

lazyfox


1 Answers

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
?>
like image 192
Pubudu Jayawardana Avatar answered Dec 29 '22 00:12

Pubudu Jayawardana