PHP: How to convert a number into a string?

Asked

Viewed 967 times

2

I have an application that generates a PDF receipt. But in the bank the values have to be converted into words

BD | PHP

10 | TEN

15 | FIFTEEN

7 | SEVEN

there is some function that returns the string of a number in php ?

1 answer

5


There is the class NumberFormatter which enables this representation of numbers to text.

$word = new \NumberFormatter('pt-BR', \NumberFormatter::SPELLOUT);
echo $word->format(13);
// treze

The second parameter of the class defines the format output type.

This class depends on the package intl to be used, check first that this package is installed with:

php -m | grep intl

If it is already installed just use it normally, if it is not installed with:

1. Linux: sudo apt-get install php*(version)*-Intl

2. Windows: remove the comment in line with extension=php_intl.dll in php.ini and restart the http server you are using

  • am using windows 10: how can I install ?

  • @alexjosesilva uncomment the extension extension=php_intl.dll in php.ini and restart the http server you are using. Reference: http://dk2.php.net/manual/en/intl.installation.php#114933

  • I put the explanation that @nunks.lol commented in the post, thanks nunks

  • Interesting this answer +1. It gets weird with broken numbers ex: 1.99

  • @rray in fact, it seems to me a lot like someone informing a number by phone "a comma nine nine nine"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.