Convert PHP amount to English

Source: Internet
Author: User

Copy codeThe 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 );
}
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];
}

}
// 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];
}
}
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.