Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert number | integer to words [duplicate]

I would like to convert integer to words, for eg: 29 would become TWENTY NINE and 50 would be FIFTY. How can I achieve this using PHP?

Here is what I have so far but it isn't giving the desired output.

$fees_so = $form_data_fees['field']['4'];
$feesInWords = strval($fees_so);
echo $feesInWords;
like image 317
sc2015 Avatar asked Oct 23 '25 19:10

sc2015


2 Answers

You can use NumberFormatter class with SPELLOUT:

$nf = new NumberFormatter("en", NumberFormatter::SPELLOUT);
echo $nf->format(1999); // one thousand nine hundred ninety-nine
like image 147
Aniket Sahrawat Avatar answered Oct 25 '25 09:10

Aniket Sahrawat


I eventually found a solution using the code from @aniket-sahrawat with a little tweaking

Here is the code in case anyone needs this in future...

<?php
$fees_so = $form_data_fees['field']['4'];
$words = filter_var($fees_so, FILTER_SANITIZE_NUMBER_INT);
$nf = new NumberFormatter("en", NumberFormatter::SPELLOUT);
echo $nf->format($words);
?>
like image 38
sc2015 Avatar answered Oct 25 '25 09:10

sc2015



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!