PHP amount number converted to English code example

Source: Internet
Author: User
Tags explode
Looking for a long time, search keywords are always lied-like.

The code is as follows:

<?php $num = 1220.01; echo Fmoney ($num);//results: 1,220.21 echo Umoney ($num); Results: one thousand and hundred twenty DOLLARS and twenty-one CENTS only echo Umoney ($num, "RMB"); Results: one thousand and hundred twenty YUAN and twenty-one FEN only//define//formatted currency function Fmoney ($num) {$num =0+ $num ; $num = sprintf ("%.02f", $num); if (strlen ($num) <= 6) return $num; Starting from the last count, it adds a "," for ($i =strlen ($num)-1, $k =1, $j =100, $i >= 0, $i--, + +) {$k _num = substr ($one, $num, 1) for each 3 digits; if ($i _ num = = ".") {$numArray [$j--] = $one _num; $k =0; continue;} if ($k%3==0 and $i!=0) {//If there are just 3 digits left, do not add ', ' $numArray [$j--] = $one _num; $numArray [$j--] = ","; $k =0;} else {$numArray [$ j--]= $one _num; }} ksort ($numArray); return join ("", $numArray);} function Umoney ($num, $type = "USD") {global $numTable, $commaTable, $moneyType;//global $numTable; $numTable [0]= "ZERO"; $numTable [1]= "one"; $numTable [2]= "; $numTable [3]= "three"; $numTable [4]= "four"; $numTable [5]= "FIVE"; $numTable [6]= "SIX"; $numTable [7]=] SEVEN "; $numTable [8]= "EIGHT"; $numTable [9]= "NINE"; $numTable [10]= "TEN"; $numTable [11]= "Eleven"; $numTable [12]= "Twelve"; $numTable [13]= "Thirteen"; $numTable [14]= "fourteen"; $numTable [15]= "fifteen"; $numTable [16]= "Sixteen"; $numTable [17]= "Seventeen"; $numTable [18]= "eighteen"; $numTable [19]= "Nineteen"; $numTable [20]= "Twenty"; $numTable [30]= "Thirty"; $numTable [40]= "Forty"; $numTable [50]= "Fifty"; $numTable [60]= "Sixty"; $numTable [70]= "Seventy"; $numTable [80]= "Eighty"; $numTable [90]= "Ninety"; $commaTable [0]= "Hundred"; $commaTable [1]= "thousand"; $commaTable [2]= "MILLION"; $commaTable [3]= "milliard"; $commaTable [4]= "billion"; $commaTable [5]=]????? "; Unit $moneyType ["USD"]= "DOLLARS"; $moneyType ["Usd_1"]= "CENTS only"; $moneyType ["RMB"]= "YUAN"; $moneyType ["Rmb_1"]= "FEN only"; if ($type = = "") $type = "USD"; $fnum = Fmoney ($num); $numArray = Explode (",", $fnum); $resultArray = Array (); $k = 0; $CC =count ($numArray); for ($i = 0; $i < count ($numArray), $i + +) {$num _str = $numArray [$I]; echo "<br>"; The processing of the decimal place 400.21 if (eregi ("\.", $num _str)) {$dotArray = Explode (".", $num _str), if ($dotArray [1]! = 0) {$resultArray [$k ++]= Format3num ($dotArray [0]+0); $resultArray [$k ++]= $moneyType [Strtolower ($type)]; $resultArray [$k ++]= "and"; $resultArray [$k ++]=format3num ($dotArray [1]+0]; $resultArray [$k ++]= $moneyType [Strtolower ($type). " _1 "]; } else {$resultArray [$k ++]=format3num ($dotArray [0]+0); $resultArray [$k ++]= $moneyType [Strtolower ($type)];}} else {//non-decimal-processing if (($num _str+0)!=0) {$resultArray [$k ++]=format3num ($num _str+0); $resultArray [$k ++]= $commaTable [--$ CC]; Judgment: Add and for ($j = $i, $j <= $cc; $j + +) {//echo "<br>";//echo $numArray [$j]; if ($numArray [$j]!=0) {$) except for decimals; resultarray[$k ++]= "and"; Break }}}}} return join ("", $resultArray);}  function Format3num ($num) {global $numTable, $commaTable; $numlen = strlen ($num); for ($i = 0, $j = 0; $i < $numlen; $i + +) {$bitenum [$j + +] = substr ($num, $i, 1);} if ($num ==0) return ""; if ($numlen = = 1) return $numTable [$num];  if ($numlen = = 2) {if ($num <=) return $numTable [$num];//First bit impossible 0 if ($bitenum [1]==0) {return $numTable [$num];} else {return trim ($numTable [$bitenum [0]*10]). " -". $numTable [$bitenum [1]]; }}//The first cannot be zero if ($numlen = = 3) {if ($bitenum [1]==0 && $bitenum [2]==0) {//100 return $numTable [$bitenum [0]]. $comm ATABLE[0]; } elseif ($bitenum [1]==0) {//102 return $numTable [$bitenum [0]]. $commaTable [0]. $numTable [$bitenum [2]];} elseif ($ bitenum[2]==0) {//120 return $numTable [$bitenum [0]]. $commaTable [0]. $numTable [$bitenum [1]*10];} else {//123 return $ numtable[$bitenum [0]]. $commaTable [0].trim ($numTable [$bitenum [1]*10]). " -". $numTable [$bitenum [2]]; }} return $num; }?>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.