Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Express Number in Words [duplicate]

Tags:

php

numbers

words

Is there a function that will express any given number in words?

For example:

If a number is 1432, then this function will return "One thousand four hundred and thirty two".

like image 680
anita.kcx Avatar asked Jul 16 '12 07:07

anita.kcx


People also ask

How to convert numbers into words in php?

php class numbertowordconvertsconver { function convert_number($number) { if (($number < 0) || ($number > 999999999)) { throw new Exception("Number is out of range"); } $giga = floor($number / 1000000); // Millions (giga) $number -= $giga * 1000000; $kilo = floor($number / 1000); // Thousands (kilo) $number -= $kilo * ...

How to convert rupees into words in php?

Example : index. =$this->single_word($lakh,"Lakh "); $thousand=(int)($this->rupees/1000); $this->rupees=$this->rupees%1000; $w. =$this->single_word($thousand,"Thousand "); $hundred=(int)($this->rupees/100); $w. =$this->single_word($hundred,"Hundred "); $ten=$this->rupees%100; $w.

Can you convert numbers into words in Excel?

Excel doesn't have a default function that displays numbers as English words in a worksheet, but you can add this capability by pasting the following SpellNumber function code into a VBA (Visual Basic for Applications) module.


2 Answers

Use the NumberFormatter class that are in php ;)

$f = new NumberFormatter("en", NumberFormatter::SPELLOUT); echo $f->format(1432); 

That would output "one thousand four hundred thirty-two"

like image 53
martindilling Avatar answered Sep 19 '22 03:09

martindilling


You can do this in many ways I am mentioning here two ways by using The NumberFormatter class as mentioned in Martindilling answer (if you have php version 5.3.0 or higher and also PECL extension 1.0.0 or higher) or by using the following custom function.

function convertNumberToWord($num = false) {     $num = str_replace(array(',', ' '), '' , trim($num));     if(! $num) {         return false;     }     $num = (int) $num;     $words = array();     $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);     for ($i = 0; $i < count($num_levels); $i++) {         $levels--;         $hundreds = (int) ($num_levels[$i] / 100);         $hundreds = ($hundreds ? ' ' . $list1[$hundreds] . ' hundred' . ' ' : '');         $tens = (int) ($num_levels[$i] % 100);         $singles = '';         if ( $tens < 20 ) {             $tens = ($tens ? ' ' . $list1[$tens] . ' ' : '' );         } else {             $tens = (int)($tens / 10);             $tens = ' ' . $list2[$tens] . ' ';             $singles = (int) ($num_levels[$i] % 10);             $singles = ' ' . $list1[$singles] . ' ';         }         $words[] = $hundreds . $tens . $singles . ( ( $levels && ( int ) ( $num_levels[$i] ) ) ? ' ' . $list3[$levels] . ' ' : '' );     } //end for loop     $commas = count($words);     if ($commas > 1) {         $commas = $commas - 1;     }     return implode(' ', $words); } 
like image 38
Shahbaz Avatar answered Sep 21 '22 03:09

Shahbaz