Convert PHP amount to English

Source: Internet
Author: User

CopyCode The Code is 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 cents only
Echo umoney (\$ num, "RMB ");
// Result: One thousand and two hundred twenty yuan and twenty-one fen only

// Define
// Format the currency
Function fmoney (\$ num ){
\$ Num = 0 + \$ num;
\$ Num = sprintf ("%. 02f", \$ num );
If (strlen (\$ num) <= 6) return \$ num;
// From the End, add one "," for each three numbers ","
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 only three digits are left, ',' is not added ','
\$ 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] = "Ty";
\$ 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 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 {
// Handle non-decimal places
If (\$ num_str + 0 )! = 0 ){
\$ Resultarray [\$ K ++] = format3num (\$ num_str + 0 );
\$ Resultarray [\$ K ++] = \$ commatable [-- \$ CC];
// Judgment: Add and if it is not zero except decimal places
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);
}< br> if (\$ num = 0) return "";
if (\$ numlen = 1) return \$ numtable [\$ num];
if (\$ numlen = 2) {
if (\$ num <= 20) return \$ numtable [\$ num];
// the first digit cannot be zero
if (\$ bitenum [1] = 0) {
return \$ numtable [\$ num];
} else {
return trim (\$ numtable [\$ bitenum [0] * 10]). "-". \$ numtable [\$ bitenum [1];
}

}< br> // The first item 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];
}< BR >}< br> return \$ num;
}< BR >?>

Related Keywords:

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.