Many a times, in our applications we need to convert number to text format. Till now, we have to write our own logic for it. But a good news for software developers that PHP has introduced a new class with name “Num2Text” that helps converting a number into respective text value.
How to Convert a Number to Text using PHP
For using this, you just need to get the required PHP class files, include it into your code and just get your purpose fulfilled.
Please go to the Link Below:
http://www.phpclasses.org/browse/package/9310/download/zip.html
And get the fresh copy of the whole code containing the Num2Text class file, along with an example to use it.
You will get it in zipped form that you need to extract to get the code files. Inside the folder you will get two version of Num2Text class:
Num2TextEng
( this class is used to get the text value of supplied number into most commonly used English language.
Num2TextSpanish
( this class is used to get the text value of supplied number into Spanish language)
And here is the example code, showing the use of Num2text classes:
1 2 3 4 5 6 7 8 9 |
<?php require_once 'Num2TextEnglish.php'; $num_to_text = new Num2TextEng(); // creating object of Num2TextEng class $num = 45; $text = $num_to_text->convertNumber($num); // using convertNumber() method of Num2TextEng class echo $text; ?> |
Output: forty-five
With this class, you can easily get text equivalent of numbers with any number of digits of your choice.
e.g. Lets take the example of a big number
1 2 3 4 5 6 7 8 9 |
<?php require_once 'Num2TextEnglish.php'; $num_to_text = new Num2TextEng(); $num = 45645645; $text = $num_to_text->convertNumber($num); // using convertNumber() method of Num2TextEng class echo $text; ?> |
Output: forty-five million, six hundred and forty-five thousand, six hundred and forty-five
Likewise, to get the text format in Spanish language, you just need to include the Spanish counterpart of Num2TextEng class.
As follows:
require_once ‘Num2TextSpanish.php’;
Hope, it will ease the developers work a bit. Thanks