PHP converts amount numbers to Chinese capitalization _php tips

Source: Internet
Author: User
Tags lowercase modulus strlen

PHP converts amount numbers to Chinese capitals

echo Tochinesenumber (1234567890);/to pick up the million thousand thousands of million Wu Qi Ba bai nine to collect round
function Tochinesenumber ($money) {
  $money = round ($ money,2);
  $cnynums = Array ("0", "one", "II", "three", "Restaurant", "WU", "Lu", "Qi", "ba", "Nine"); 
  $cnyunits = Array ("Round", "Corner", "cent");
  $cnygrees = Array ("Pick up", "Bai", "Qian", "Million", "Pick Up", "Bai", "thousand", "billion"); 
  List ($int, $dec) = Explode (".", $money, 2);
  $dec = Array_filter (Array ($dec [1], $dec [0])); 
  $ret = Array_merge ($dec, Array (Implode ("", Cnymapunit (Str_split ($int), $cnygrees)); 
  $ret = Implode ("", Array_reverse (Cnymapunit ($ret, $cnyunits)); 
  Return Str_replace (Array_keys ($cnynums), $cnynums, $ret);
function Cnymapunit ($list, $units) { 
  $ul =count ($units); 
  $xs =array (); 
  foreach (Array_reverse ($list) as $x) { 
    $l =count ($XS); 
    if ($x!= "0" | |! ( $l%4) 
      $n = ($x = = ' 0 '? ': $x). ( $units [($l-1)% $ul]); 
    else $n =is_numeric ($xs [0][0])? $x: '; 
 Array_unshift ($xs, $n); 
 } 
 return $xs; 
 }

Code two:

/** * Numeric amount converted to Chinese capital amount function *string Int $num lowercase or lowercase strings to be converted *return uppercase letters * Decimal places are two-bit **/function NUM_TO_RMB ($num) {$c 1 = "0"
    Restaurant Woolu Qi Ba Nine ";
    $c 2 = "cent of the points of the hundred million thousand thousand million"; 
    Accurate to the back of the point is not, so leave only two decimal places $num = Round ($num, 2);
    Converts a number to an integer $num = $num * 100;
    if (strlen ($num) > a) {return "The amount is too large, please check";
    } $i = 0;
    $c = "";
        while (1) {if ($i = = 0) {//get last digit $n = substr ($num, strlen ($num)-1, 1);
        else {$n = $num% 10;
        ///each time the last number is translated into Chinese $p 1 = substr ($c 1, 3 * $n, 3);
        $p 2 = substr ($c 2, 3 * $i, 3); if ($n!= ' 0 ' | | ($n = = ' 0 ' && ($p 2 = ' billion ' | | $p 2 = ' million ' | | $p 2 = = ')])
        {$c = $p 1. $p 2. $c;
        else {$c = $p 1. $c;
        } $i = $i + 1;
        Remove the number the last one $num = $num/10;
        $num = (int) $num;
        End Loop if ($num = = 0) {break;
    }} $j = 0;
    $slen = strlen ($c); WhilE ($j < $slen) {//utf8 A character of quite 3 characters $m = substr ($c, $j, 6); Deal with many 0 of the number of cases, each cycle to remove a Chinese character "0" if ($m = = ' 0 ' | | | $m = = ' 0 ' | | | $m = = ' 0 ' | | $m = = ' 00 ') {$left = substr
            ($c, 0, $j);
            $right = substr ($c, $j + 3); $c = $left.
            $right;
            $j = $j-3;
        $slen = $slen-3;
    } $j = $j + 3;
    //This is to remove the last "0" word in the 23.0 (substr ($c, strlen ($c)-3, 3) = = ' 0 ') {$c = substr ($c, 0, strlen ($c)-3);
    ///Add the processed Chinese character "whole" if (empty ($c)) {return "0 yuan"; }else{return $c.
    "Whole"; } Echo NUM_TO_RMB (23000000.00); Three thousand million Yuan whole

Code Three:

<?php//Post a number to Chinese first, up to 12 digits function convert_2_cn ($num) {$convert _CN = array ("0", "one", "two", "three", "Restaurant", "WU", "Lu", "Qi", "ba", "Nine
");
$repair _number = Array (' 0 thousand 0 bai 0 titbits ', ' absolutely ', ' 0 thousand ', ' 0 bai ', ' 0 pick ');
$unit _CN = Array ("Pick up", "Bai", "Qian", "million", "billion");
$exp _CN = Array ("", "Million", "billion");

$max _len = 12;
$len = strlen ($num);
if ($len > $max _len) {return ' outnumber ';} $num = Str_pad ($num,,, '-', str_pad_left);
$exp _num = Array ();
$k = 0;
For ($i =12 $i >0; $i-) {if ($i%4 = 0) {$k + +;} $exp _num[$k] = substr ($num, $i -1,1);} $str = '; foreach ($exp _num as $key => $nums) {if (Array_sum ($nums)) {$str = Array_shift ($exp _cn). $str; foreach ($nums as $nk =&gt $NV) {if ($nv = = ') {continue} if ($nk = = 0) {$str = $convert _cn[$nv]. $str;} else {$str = $convert _cn[$nv]. $unit _CN [$nk-1].
$STR;
}} $str = Str_replace ($repair _number,array (' million ', ' billion ', '-'), $STR);
$str = Preg_replace ("/-{2,}/", "", $str);
$str = str_replace (Array (' 0 ', '-'), Array (', ' 0 '), $STR);
return $str; The Echo convert_2_cn (1111).
\ n "; echo CONVERT_2_CN (111111). "
\ n "; EcHo convert_2_cn (111111111111). "

\ n "; Add a function Cn_2_num ($str) {$convert _CN = array ("0", "one", "" II "," three "," Restaurant "," WU "," Lu "," Qi "," ba "," Nine "); $skip _words =
Array ("Pick up", "Bai", "thousand");
$str = Str_replace ($skip _words, "", $str);
$len = Mb_strlen ($str, ' utf-8 ');
$num = 0;
$k = '; For ($i =0 $i < $len; $i + +) {$CN = Mb_substr ($str, $i, 1, ' utf-8 '); if ($CN = = ' billion ') {$num = $num + intval ($k) *100000000; $k =
'';
} elseif ($CN = = ' million ') {$num = $num + intval ($k) *10000; $k = ';} else {$k = $k. Array_search ($CN, $convert _cn);}    
if ($k) {$num = $num + intval ($k);}                                                            
return $num; Echo cn_2_num (' One thousand one hundred million one billion one million one thousand one hundred one thousand one to pick up a ")."                                 
\ n "; Echo Cn_2_num (' Pick one thousand one thousand one to pick one '). "  
\ n "; ?>

Code four:

function ConvertCurrency (currencydigits) {//Constants:var maximum_number = 99999999999.99;//predefine the Radix char
Acters and currency symbols for output:var Cn_zero = "0";
var cn_one = "one";
var cn_two = "II";
var cn_three = "three";
var cn_four = "Restaurant";
var cn_five = "WU";
var cn_six = "Lu";
var cn_seven = "Qi";
var cn_eight = "ba";
var cn_nine = "Nine";
var Cn_ten = "Pick up";
var cn_hundred = "Bai";
var Cn_thousand = "Qian";
var cn_ten_thousand = "million";
var cn_hundred_million = "billion";
var cn_symbol = "RMB";
var cn_dollar = "Yuan";
var cn_ten_cent = "Corner";
var cn_cent = "min";
var Cn_integer = "whole"; Variables:var Integral; 
represent integral part of digit. var decimal;
represent decimal part of digit number. var outputcharacters;
The output result.
var parts;
var digits, radices, bigradices, decimals;
var Zerocount;
var i, p, D;
var quotient, modulus;
Validate Input string:currencydigits = currencydigits.tostring ();
 if (currencydigits = = "") {alert ("Empty input!");
Return "";} if (CURrencydigits.match (/[^,.\d]/)!= null) {alert ("Invalid characters in the input string!");
Return "";} if ((currencydigits). Match (/^ (\d{1,3} (, \d{3}) * (. ( (\d{3},) *\d{1,3})?) | (\d+ (. \d+)?))
 $/) = = null) {alert ("illegal format of digit number!");
Return "";} Normalize the format of input digits:currencydigits = Currencydigits.replace (/,/g, "");
Remove comma delimiters. CurrencyDigits = Currencydigits.replace (/^0+/, ""); 
Trim Zeros at the beginning.
Assert the number is not greater than the maximum number.
 if (number (currencydigits) > Maximum_number) {alert ("Too large a number to convert!");
Return "";} Http://www.knowsky.com/Process the coversion from currency digits to characters://separate integral and decimal part
s before processing coversion:parts = Currencydigits.split (".");
 if (Parts.length > 1) {integral = parts[0];
 decimal = parts[1];
 Cut down redundant decimal digits this are after the second.
decimal = Decimal.substr (0, 2);
 } else {integral = parts[0];
decimal = "";} Prepare the characters corresponding to the digits:digits = new Array (Cn_zero, Cn_one, Cn_two, Cn_three, Cn_four, Cn_f
IVE, Cn_six, Cn_seven, cn_eight,cn_nine);
radices = new Array ("", Cn_ten, cn_hundred, Cn_thousand);
Bigradices = new Array ("", Cn_ten_thousand, cn_hundred_million);
decimals = new Array (cn_ten_cent, cn_cent);
Start processing:outputcharacters = "";
 Process integral part if it is larger than 0:if (number (integral) > 0) {zerocount = 0;
  for (i = 0; i < integral.length i++) {p = integral.length-i-1;
  D = Integral.substr (i, 1);
  quotient = P/4;
  modulus = p% 4;
  if (d = = "0") {zerocount++;
  else {if (Zerocount > 0) {outputcharacters + = digits[0];
  } zerocount = 0;
  Outputcharacters + = Digits[number (d)] + radices[modulus];
  } if (modulus = = 0 && Zerocount < 4) {outputcharacters + = bigradices[quotient];
} outputcharacters + = Cn_dollar; }//Process decimalPart if there is:if (decimal!= "") {for (i = 0; i < decimal.length;
  i++) {d = decimal.substr (i, 1);
  if (d!= "0") {outputcharacters + = Digits[number (d)] + decimals[i]; }}//Confirm and return the final output string:if (outputcharacters = "") {outputcharacters = Cn_zero + Cn_dolla
R
} if (decimal = = "") {outputcharacters + = Cn_integer;}
Outputcharacters = Cn_symbol + outputcharacters;
Outputcharacters = outputcharacters;
return outputcharacters;
}//var stmp = ""; function Nst_convert (t) {if (t.value==stmp) return;//returns the var ms = T.value.replace (/[^\d\.] If equal to the last input.
  /g, ""). Replace (/(\.\d{2}). +$/, "$". Replace (/^0+ ([1-9])/, "$"). Replace (/^0+$/, "0"); Replace (/[^\d\.] /g, "") remove input which is not numeric and. The character//replace (/(\.\d{2}). +$/, "$"///Match the first from the character. All subsequent characters, since no G tag was used,//So only the first match was started and then replaced with the decimal and the last two digits
  Determines that the final format of the value is correct and high.
  Replace (/^0+ ([1-9])/, "$") matches the number beginning with a value of more than 0 to remove the value after 0 as the first digit is also the beginning of the match. Replace (/^0+$/, "0") matches 0 start and end of multiple 0 for a 0 i.e. 0000000 input-> converted to a 0//below to determine the input for the filtered fitMethod Digital//alert (MS);
  var txt = ms.split (".");
  Alert (txt[0]); If the MS value does not exist, the value before the decimal point is equal to the txt[0]= of Ms//regexp:/\d{4} (, |$)/matching four-digit numbers and, the set of four-digit and character endings while (/\d{4} (, |$)/.test (txt[0))
  )//If txt[0]=4123 txt[0] = txt[0].replace (/(\d) (\d{3} (, |$))/, "$1,$2"); Txt[0].replace (/(\d) (\d{3} (, |$))/, "$1,$2" is then assigned to Txt[0 (//regexp:/) (\d} (, \d{3))/four digits to the first digit of the two groups,  The last three digits and the other end for every two bits//and replaced first, second note, the use of very good. That is, replace 4123 with 4,123//Because this expression defaults to a greedy match, so the forward match from the value and then through the loop to match the replacement so that the//12345678 can be divided into the 123,456,78 you want (, |$) is very fine, because it omitted the second match , question T.value = Stmp = txt[0]+ (txt.length>1? "."
  +TXT[1]: "");
  The final assignment to the input box//If there is a decimal point plus merge into the final number otherwise show the replacement txt[0] Bbb.value = convertcurrency (ms-0); Convert MS to digital send to NUMBER2NUM1 to convert}

The above is the entire contents of this article, I hope you can enjoy.

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.