Convert a number to its string representation

I am developing a simple web application where I need to display the number a for my users in a string format.

Example:

12 - One Two or Twelve -20 - minus Two zero or minus twenty 

In any case, this is normal. I need this to be done in PHP. Any help would be appreciated.

+4
source share
4 answers

for the first option (spell digits), strtr is your friend

 $words = array( '-' => 'minus ', '1' => 'one ', '2' => 'two ', etc.... ); echo strtr(-123, $words); 
+7
source

If you want to specify the full number, you can use the PEAR class Numbers_Words . This class has a toWords () method that takes a number +ve or -ve num and returns a string representation of the number.

If you want to convert a number to a digit wise string, I don't know about any lib function. But you can easily encode one. user187291 gives a good way to do this in your answer .

 <?php $arr = array( -12, 20 ); foreach($arr as $num) { $nw = new Numbers_Words(); echo "$num = ". $nw->toWords($num)."\n"; } ?> 

Conclusion:

 C:\>php a.php -12 = minus twelve 20 = twenty 
+3
source

Below I give you an example function. This may not be complete, but you need to start (I know the question was posted a long time ago, but it may help others). And I'm sorry for any mistakes :). and finally it is not finished yet. I will just post an example for starting point.

 function convertToString($number, $blankIfZero=true){ $strRep = ""; $n = intval($number); $one2twenty = array("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"); $twenty2ninty = array("Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"); $hundred = "Hundred"; $thousand = "Thousand"; $million = "Million"; $billion = "Billion"; switch($n){ case 0: if($blankIfZero == true){ $strRep= $strRep.""; break; }else{ $strRep = $strRep."Zero"; break; } case $n >0 && $n <20: $strRep = $strRep." ".$one2twenty[($n-1)]; break; case $n >=20 && $n < 100: $strRep = $strRep . " ". $twenty2ninty[(($n/10) - 2)]; $strRep .= convertToString($n%10); break; case $n >= 100 && $n <= 999: $strRep = $strRep.$one2twenty[(($n/100)-1)]." ".$hundred. " "; $strRep .= convertToString($n%100); break; case $n >= 1000 && $n < 100000: if($n < 20000){ $strRep = $strRep.$one2twenty[(($n/1000)-1)]." ".$thousand. " "; $strRep .= convertToString($n%1000); break; }else{ $strRep = $strRep . $twenty2ninty[(($n/10000) - 2)]; $strRep .= convertToString($n%10000); break; } case $n >= 100000 && $n < 1000000: $strRep .= convertToString($n/1000). " ".$thousand. " "; $strRep .= convertToString(($n%100000)%1000); break; case $n >= 1000000 && $n < 10000000: $strRep = $strRep . $one2twenty[(($n/1000000) - 1)]. " ".$million." "; $strRep .= convertToString($n%1000000); break; case $n >= 10000000 && $n < 10000000000: $strRep .= convertToString($n/1000000). " ".$million. " "; $strRep .= convertToString(($n%1000000)); break; } return $strRep; } 
+1
source

Based on the work that @SRC did in the previous answer, I wrote a function to convert any raw unsigned integer to English, mostly out of curiosity.

You can find the code here: GitHub Gist from StampyCode

This code is limited only by the PHP Integer (64-bit) restriction:

 9223372036854775807 

What is processed by this code for output:

nine quintillion, two hundred twenty three quadrillion, three hundred seventy two trillion, thirty six billion eight hundred fifty four million seven hundred seventy five thousand eight hundred and seven.

Code insertion:

 <?php $_1to19 = [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", ]; $_teen = [ "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", ]; $_mult = [ 2 => 'hundred', 3 => 'thousand', 6 => 'million', 9 => 'billion', 12 => 'trillion', 15 => 'quadrillion', 18 => 'quintillion', 21 => 'sextillion', 24 => 'septillion', // php can't count this high 27 => 'octillion', ]; $fnBase = function ($n, $x) use (&$fn, $_mult) { return $fn($n / (10 ** $x)) . ' ' . $_mult[$x]; }; $fnOne = function ($n, $x) use (&$fn, &$fnBase) { $y = ($n % (10 ** $x)) % (10 ** $x); $s = $fn($y); $sep = ($x === 2 && $s ? " and " : ($y < 100 ? ($y ? " and " : '') : ', ')); return $fnBase($n, $x) . $sep . $s; }; $fnHundred = function ($n, $x) use (&$fn, &$fnBase) { $y = $n % (10 ** $x); $sep = ($y < 100 ? ($y ? ' and ' : '') : ', '); return ', ' . $fnBase($n, $x) . $sep . $fn($y); }; $fn = function ($n) use (&$fn, $_1to19, $_teen, $number, &$fnOne, &$fnHundred) { switch ($n) { case 0: return ($number > 1 ? '' : 'zero'); case $n < 20: return $_1to19[$n - 1]; case $n < 100: return $_teen[($n / 10) - 2] . ' ' . $fn($n % 10); case $n < (10 ** 3): return $fnOne($n, 2); }; for ($i = 4; $i < 27; ++$i) { if ($n < (10 ** $i)) { break; } } return ($i % 3) ? $fnHundred($n, $i - ($i % 3)) : $fnOne($n, $i - 3); }; $number = $fn((int)$number); $number = str_replace(', , ', ', ', $number); $number = str_replace(', ', ', ', $number); $number = str_replace(' ', ' ', $number); $number = ltrim($number, ', '); return $number; 
+1
source

Source: https://habr.com/ru/post/1304147/