PHP amount number converted into English _php tips

Source: Internet
Author: User
Copy Code code as follows:

<?php
$num = 1220.01;
echo Fmoney ($num);//Result: 1,220.21
echo Umoney ($num);
Result: one thousand and two hundred twenty dollars and twenty-one only
Echo Umoney ($num, "RMB");
Result: one thousand and two hundred twenty YUAN and twenty-one only

Define
Format currency
function Fmoney ($num) {
$num =0+ $num;
$num = sprintf ("%.02f", $num);
if (strlen ($num) <= 6) return $num;
From the last count, every 3 digits it adds a ","
For ($i =strlen ($num)-1, $k =1, $j =100; $i >= 0; $i--, $k + +) {
$one _num = substr ($num, $i, 1);
if ($one _num = = ".") {
$numArray [$j-] = $one _num;
$k = 0;
Continue
}

if ($k%3==0 and $i!=0) {
If there are just 3 digits left, no ', '
$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]= "two";
$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>";
Processing of decimal places 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 {
Processing of non-decimal places
if (($num _str+0)!=0) {
$resultArray [$k ++]=format3num ($num _str+0);
$resultArray [$k ++]= $commaTable [-$CC];
Judgment: Plus and if the rest is not zero except for the decimal
for ($j = $i; $j <= $cc; $j + +) {
echo "<br>";
echo $numArray [$j];
if ($numArray [$j]!=0) {
$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 place cannot be zero
if ($bitenum [1]==0) {
return $numTable [$num];
} else {
return Trim ($numTable [$bitenum [0]*10]). " -". $numTable [$bitenum [1]];
}

}
The first one cannot be zero
if ($numlen = = 3) {
if ($bitenum [1]==0 && $bitenum [2]==0] {
100
return $numTable [$bitenum [0]]. $commaTable [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;
}
?>

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.