- PHP — Convert Number into Word
- Saved searches
- Use saved searches to filter your results more quickly
- License
- kwn/number-to-words
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- How to Convert Number into Words in PHP
- If you have any comments or suggestions about the How to Convert Number into Words in PHP Please message us Directly:
- How To Convert Number To Words In PHP
- Number-to-Word Conversion with PHP
- Dependencies
- Sources
- Usage
PHP — Convert Number into Word
Now let’s see example of how to convert number into word using php. i am going to learn you convert number to word in php. we will show number into word convert using php. This tutorial will give you how to convert number to word in php.
In this blog, i will show you convert number into word in php. Here i will give you full example for convert number into word using php.
In this example i am create form in one input to enter your number into convert in word using php. So let’s see the bellow example:
How to Convert Number into Words in PHP — NiceSnippets.com
$word = convertNumberToWordsForIndia($_POST[‘text-field’]);
if (!empty($word)) <
echo ‘
‘;
>
?>
function convertNumberToWordsForIndia($number) <
$words = array(
‘0’=> » ,’1’=> ‘one’ ,’2’=> ‘two’ ,’3′ => ‘three’,’4′ => ‘four’,’5′ => ‘five’,
‘6’ => ‘six’,’7′ => ‘seven’,’8′ => ‘eight’,’9′ => ‘nine’,’10’ => ‘ten’,
’11’ => ‘eleven’,’12’ => ‘twelve’,’13’ => ‘thirteen’,’14’ => ‘fouteen’,’15’ => ‘fifteen’,
’16’ => ‘sixteen’,’17’ => ‘seventeen’,’18’ => ‘eighteen’,’19’ => ‘nineteen’,’20’ => ‘twenty’,
’30’ => ‘thirty’,’40’ => ‘fourty’,’50’ => ‘fifty’,’60’ => ‘sixty’,’70’ => ‘seventy’,
’80’ => ‘eighty’,’90’ => ‘ninty’);
//First find the length of the number
$number_length = strlen($number);
//Initialize an empty array
$number_array = array(0,0,0,0,0,0,0,0,0);
$received_number_array = array();
//Store all received numbers into an array
for($i=0;$i <$number_length;$i++)<
$received_number_array[$i] = substr($number,$i,1);
>
//Populate the empty array with the numbers received — most critical operation
for($i=9-$number_length,$j=0;$i <9;$i++,$j++)<
$number_array[$i] = $received_number_array[$j];
>
$number_to_words_string = «»;
//Finding out whether it is teen ? and then multiply by 10, example 17 is seventeen, so if 1 is preceeded with 7 multiply 1 by 10 and add 7 to it.
for($i=0,$j=1;$i <9;$i++,$j++)<
//»01,23,45,6,78″
//»00,10,06,7,42″
//»00,01,90,0,00″
if($i==0 || $i==2 || $i==4 || $i==7) <
if($number_array[$j]==0 || $number_array[$i] == «1») <
$number_array[$j] = intval($number_array[$i])*10+$number_array[$j];
$number_array[$i] = 0;
>
>
>
$value = «»;
for($i=0;$i <9;$i++)<
if($i==0 || $i==2 || $i==4 || $i==7) <
$value = $number_array[$i]*10;
>
else <
$value = $number_array[$i];
>
if($value!=0) < $number_to_words_string.= $words["$value"]." "; >
if($i==1 && $value!=0) < $number_to_words_string.= "Crores "; >
if($i==3 && $value!=0) < $number_to_words_string.= "Lakhs "; >
if($i==5 && $value!=0) < $number_to_words_string.= "Thousand "; >
if($i==6 && $value!=0) < $number_to_words_string.= "Hundred & "; >
>
if($number_length>9)
return $number_to_words_string;
>
?>
✌️ Like this article? Follow me on Twitter and Facebook. You can also subscribe to RSS Feed.
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Number to string standalone PHP library with i18n. Drivers for numbers and currency included.
License
kwn/number-to-words
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
PHP Number to words converter
This library converts numbers to their word representation (123 -> one hundred twenty three).
Add package to your composer.json by running:
$ composer require kwn/number-to-words
There are two types of number-to-words transformation: number and currency. In order to use a relevant transformer for specific language create an instance of NumberToWords class and call a method that creates a new instance of the desired transformer;
Create a transformer for specific language using the getNumberTransformer(‘lang’) method:
use NumberToWords\NumberToWords; // create the number to words "manager" class $numberToWords = new NumberToWords(); // build a new number transformer using the RFC 3066 language identifier $numberTransformer = $numberToWords->getNumberTransformer('en');
Transformer can be used by passing in numeric values to the toWords() method:
$numberTransformer->toWords(5120); // outputs "five thousand one hundred twenty"
It can be also used with a static method:
NumberToWords::transformNumber('en', 5120); // outputs "five thousand one hundred twenty"
Creating a currency transformer works just like a number transformer.
use NumberToWords\NumberToWords; // create the number to words "manager" class $numberToWords = new NumberToWords(); // build a new currency transformer using the RFC 3066 language identifier $currencyTransformer = $numberToWords->getCurrencyTransformer('en');
Then it can be used passing in numeric values for amount and ISO 4217 currency identifier to the toWords() method:
$currencyTransformer->toWords(5099, 'USD'); // outputs "fifty dollars ninety nine cents"
It can be also used with a static method:
NumberToWords::transformCurrency('en', 5099, 'USD'); // outputs "fifty dollars ninety nine cents"
Please keep in mind, the currency transformer accepts integers as the amount to transform. It means that if you store amounts as floats (e.g. 4.99) you need to multiply them by 100 and pass the integer (499) as an argument.
Language | Identifier | Number | Currency |
---|---|---|---|
Albanian | al | + | + |
Arabic | ar | + | + |
Azerbaijani | az | + | + |
Belgian French | fr_BE | + | — |
Brazilian Portuguese | pt_BR | + | + |
Bulgarian | bg | + | — |
Czech | cs | + | — |
Danish | dk | + | + |
Dutch | nl | + | — |
English | en | + | + |
Estonian | et | + | — |
Georgian | ka | + | + |
German | de | + | + |
French | fr | + | + |
Hungarian | hu | + | + |
Indonesian | id | + | + |
Italian | it | + | — |
Kurdish | ku | + | — |
Lithuanian | lt | + | + |
Latvian | lv | + | + |
Macedonian | mk | + | — |
Malay | ms | + | + |
Persian | fa | + | — |
Polish | pl | + | + |
Romanian | ro | + | + |
Slovak | sk | + | + |
Spanish | es | + | + |
Russian | ru | + | + |
Swedish | sv | + | — |
Turkish | tr | + | + |
Turkmen | tk | + | + |
Ukrainian | ua | + | + |
Yoruba | yo | + | + |
Many transformers were ported from the pear/Numbers_Words library. Some of them were created from scratch by contributors. Thank you!
Version 2.x — BC and major changes
Q: I found a bug. What should I do?
A: Please report an issue on GitHub. Feel free to fix it and open a pull request. I don’t know most of those languages that the library supports, so your help and contribution would be much appreciated. Thanks!
Q: My language is missing. Could it be added?
A: Unfortunately, there’s a high chance I don’t know your language. Feel free to implement the missing transformer and open a pull request. You can take a look at the existing transformers, and follow the same pattern as other languages do.
About
Number to string standalone PHP library with i18n. Drivers for numbers and currency included.
How to Convert Number into Words in PHP
Hello Guys, This tutorial is all about How to Convert Number into Words in PHP. This tutorial is very easy and helpful as you will learn How to Convert Number into Words in PHP. So let’s get started:
First is to create a class name it “index.php” and put this code:
Equivalent in words: 'Zero', 1 => 'One', 2 => 'Two', 3 => 'Three', 4 => 'Four', 5 => 'Five', 6 => 'Six', 7 => 'Seven', 8 => 'Eight', 9 => 'Nine', 10 => 'Ten', 11 => 'Eleven', 12 => 'Twelve', 13 => 'Thirteen', 14 => 'Fourteen', 15 => 'Fifteen', 16 => 'Sixteen', 17 => 'Seventeen', 18 => 'Eighteen', 19 => 'Nineteen', 20 => 'Twenty', 30 => 'Thirty', 40 => 'Fourty', 50 => 'Fifty', 60 => 'Sixty', 70 => 'Seventy', 80 => 'Eighty', 90 => 'Ninety', 100 => 'Hundred', 1000 => 'Thousand', 1000000 => 'Million', 1000000000 => 'Billion', 1000000000000 => 'Trillion', 1000000000000000 => 'Quadrillion', 1000000000000000000 => 'Quintillion' ); if (!is_numeric($number)) < return false; >if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) < // overflow trigger_error( 'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX, E_USER_WARNING ); return false; >if ($number < 0) < return $negative . convert_number_to_words(abs($number)); >$string = $fraction = null; if (strpos($number, '.') !== false) < list($number, $fraction) = explode('.', $number); >switch (true) < case $number < 21: $string = $dictionary[$number]; break; case $number < 100: $tens = ((int) ($number / 10)) * 10; $units = $number % 10; $string = $dictionary[$tens]; if ($units) < $string .= $hyphen . $dictionary[$units]; >break; case $number < 1000: $hundreds = $number / 100; $remainder = $number % 100; $string = $dictionary[$hundreds] . ' ' . $dictionary[100]; if ($remainder) < $string .= $conjunction . convert_number_to_words($remainder); >break; default: $baseUnit = pow(1000, floor(log($number, 1000))); $numBaseUnits = (int) ($number / $baseUnit); $remainder = $number % $baseUnit; $string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit]; if ($remainder) < $string .= $remainder < 100 ? $conjunction : $separator; $string .= convert_number_to_words($remainder); >break; > if (null !== $fraction && is_numeric($fraction)) < $string .= $decimal; $words = array(); foreach (str_split((string) $fraction) as $number) < $words[] = $dictionary[$number]; >$string .= implode(' ', $words); > return $string; > echo ''.convert_number_to_words($_POST['textfield']).''; ?>
Simple as that. Go test the program!
If you have any comments or suggestions about the How to Convert Number into Words in PHP Please message us Directly:
How To Convert Number To Words In PHP
In this PHP tutorial, I will show you a simple way to convert numbers to words in PHP. Sometimes we need to convert decimal numbers to words in PHP applications like when we create invoices, we need to show numbers in words format in many places.
So I am here to show you the way of converting numbers into words in PHP. Assume we do input 1738832 then we will have an output like One Million and Seven Hundreds Thirty Eight Thousand and Eight Hundreds Thirty Two:
This is the thing that we are going to show. See the below example of how to convert number to words in php:
function number_to_word( $num = '' ) < $num = ( string ) ( ( int ) $num ); if( ( int ) ( $num ) && ctype_digit( $num ) ) < $words = array( ); $num = str_replace( array( ',' , ' ' ) , '' , trim( $num ) ); $list1 = array('','one','two','three','four','five','six','seven', 'eight','nine','ten','eleven','twelve','thirteen','fourteen', 'fifteen','sixteen','seventeen','eighteen','nineteen'); $list2 = array('','ten','twenty','thirty','forty','fifty','sixty', 'seventy','eighty','ninety','hundred'); $list3 = array('','thousand','million','billion','trillion', 'quadrillion','quintillion','sextillion','septillion', 'octillion','nonillion','decillion','undecillion', 'duodecillion','tredecillion','quattuordecillion', 'quindecillion','sexdecillion','septendecillion', 'octodecillion','novemdecillion','vigintillion'); $num_length = strlen( $num ); $levels = ( int ) ( ( $num_length + 2 ) / 3 ); $max_length = $levels * 3; $num = substr( '00'.$num , -$max_length ); $num_levels = str_split( $num , 3 ); foreach( $num_levels as $num_part ) < $levels--; $hundreds = ( int ) ( $num_part / 100 ); $hundreds = ( $hundreds ? ' ' . $list1[$hundreds] . ' Hundred' . ( $hundreds == 1 ? '' : 's' ) . ' ' : '' ); $tens = ( int ) ( $num_part % 100 ); $singles = ''; if( $tens < 20 ) < $tens = ( $tens ? ' ' . $list1[$tens] . ' ' : '' ); >else < $tens = ( int ) ( $tens / 10 ); $tens = ' ' . $list2[$tens] . ' '; $singles = ( int ) ( $num_part % 10 ); $singles = ' ' . $list1[$singles] . ' '; >$words[] = $hundreds . $tens . $singles . ( ( $levels && ( int ) ( $num_part ) ) ? ' ' . $list3[$levels] . ' ' : '' ); > $commas = count( $words ); if( $commas > 1 ) < $commas = $commas - 1; >$words = implode( ', ' , $words ); $words = trim( str_replace( ' ,' , ',' , ucwords( $words ) ) , ', ' ); if( $commas ) < $words = str_replace( ',' , ' and' , $words ); >return $words; > else if( ! ( ( int ) $num ) ) < return 'Zero'; >return ''; > echo number_to_word(1738832);
Hope this convert number to words in PHP tutorial will help you.
Number-to-Word Conversion with PHP
Often it is required to convert a number or integer to English Words for printing on invoices or for displaying final bill on checkout pages of online shopping carts.
This function accepts integer or string representation of integer and converts to English words. This can be helpful for your online shopping carts, and for generating invoices online.
function number_to_word( $num = '' ) < $num = ( string ) ( ( int ) $num ); if( ( int ) ( $num ) && ctype_digit( $num ) ) < $words = array( ); $num = str_replace( array( ',' , ' ' ) , '' , trim( $num ) ); $list1 = array('','one','two','three','four','five','six','seven', 'eight','nine','ten','eleven','twelve','thirteen','fourteen', 'fifteen','sixteen','seventeen','eighteen','nineteen'); $list2 = array('','ten','twenty','thirty','forty','fifty','sixty', 'seventy','eighty','ninety','hundred'); $list3 = array('','thousand','million','billion','trillion', 'quadrillion','quintillion','sextillion','septillion', 'octillion','nonillion','decillion','undecillion', 'duodecillion','tredecillion','quattuordecillion', 'quindecillion','sexdecillion','septendecillion', 'octodecillion','novemdecillion','vigintillion'); $num_length = strlen( $num ); $levels = ( int ) ( ( $num_length + 2 ) / 3 ); $max_length = $levels * 3; $num = substr( '00'.$num , -$max_length ); $num_levels = str_split( $num , 3 ); foreach( $num_levels as $num_part ) < $levels--; $hundreds = ( int ) ( $num_part / 100 ); $hundreds = ( $hundreds ? ' ' . $list1[$hundreds] . ' Hundred' . ( $hundreds == 1 ? '' : 's' ) . ' ' : '' ); $tens = ( int ) ( $num_part % 100 ); $singles = ''; if( $tens < 20 ) < $tens = ( $tens ? ' ' . $list1[$tens] . ' ' : '' ); >else < $tens = ( int ) ( $tens / 10 ); $tens = ' ' . $list2[$tens] . ' '; $singles = ( int ) ( $num_part % 10 ); $singles = ' ' . $list1[$singles] . ' '; >$words[] = $hundreds . $tens . $singles . ( ( $levels && ( int ) ( $num_part ) ) ? ' ' . $list3[$levels] . ' ' : '' ); > $commas = count( $words ); if( $commas > 1 ) < $commas = $commas - 1; >$words = implode( ', ' , $words ); //Some Finishing Touch //Replacing multiples of spaces with one space $words = trim( str_replace( ' ,' , ',' , trim_all( ucwords( $words ) ) ) , ', ' ); if( $commas ) < $words = str_replace_last( ',' , ' and' , $words ); >return $words; > else if( ! ( ( int ) $num ) ) < return 'Zero'; >return ''; >
Dependencies
number_to_word() requires two additional functions trim_all() and str_replace_last() .
Sources
Names of Large Numbers collected from wikipedia.
Usage
echo number_to_word( '2281941596' ); //Sample output - number_to_word Two Billion, Two Hundreds Eighty One Million, Nine Hundreds Forty One Thousand and Five Hundreds Ninety Six
Proudly powered by WordPress. Theme: Flat 1.7.11 by Themeisle. CDN by PageCDN.
Website developed by Web Developers in Lahore..