PHP converts amount numbers to Chinese capitalization, _php tutorial

Source: Internet
Author: User
Tags modulus

PHP converts amount numbers to Chinese capitals,


PHP converts amount numbers to Chinese capitals

echo Tochinesenumber (1234567890);//One to pick up the hundred billion three thousand of thousands of wood to pick up the land million BA Hundred nine round function Tochinesenumber ($money) {  $money = round ($money, 2);  $cnynums = Array ("0", "one", "II", "three", "the", "WU", "Lu", "Qi", "ba", "JIU");   $cnyunits = Array ("Circle", "angle", "min");  $cnygrees = Array ("Pick", "Bai", "Qian", "Million", "Pick", "Bai", "Qian", "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:

/*** A function that converts a numeric amount to a Chinese capital amount *string Int $num the lowercase or lowercase string to convert *return uppercase * Decimal places are two-bit **/function NUM_TO_RMB ($num) {$c 1 = "0 One and three Woolu seven BA Nine"    ;    $c 2 = "split-angle element pick Bai thousand thousand million";     Accurate to the point after the not, so only two decimal digits $num = Round ($num, 2);    Converts a number to an integer $num = $num * 100;    if (strlen ($num) >) {return "amount is too large, please check";    } $i = 0;    $c = "";        while (1) {if ($i = = 0) {//Gets the last digit $n = substr ($num, strlen ($num)-1, 1);        } else {$n = $num% 10;        }//each time the last digit is converted to 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 = = ' Yuan ')))        {$c = $p 1. $p 2. $c;        } else {$c = $p 1. $c;        } $i = $i + 1;        Minus the last digit of the number $num = $num/10;        $num = (int) $num;        End Loop if ($num = = 0) {break;    }} $j = 0;    $slen = strlen ($c);  while ($j < $slen) {//utf8 A kanji of 3 characters      $m = substr ($c, $j, 6); Deal with a lot of 0 of the number of cases, each cycle to remove a kanji "0" if ($m = = ' 0 Yuan ' | | $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 similar 23.0 in the last "0" word if (substr ($c, strlen ($c)-3, 3) = = ' 0 ') {$c = substr ($c, 0, strlen ($c)-3);    }//The kanji will be processed with an "integer" If (Empty ($c)) {return "0 yuan whole"; }else{return $c.    "Whole"; }}echo NUM_TO_RMB (23000000.00); Three hundred thousand million whole

Code Three:

<?php//first to paste a number to Chinese, up to 12-digit function CONVERT_2_CN ($num) {$convert _CN = array ("0", "one", "II", "three", "Place", "WU", "Lu", "Qi", "ba", "JIU $repair _number = Array (' 0 thousand 0 bai 0 titbits ', ' absolute ', ' 0 thousand ', ' 0 bai ', ' 0 Pick '), $unit _cn = Array ("Pick", "Bai", "Thousand", "million", "billion"), $exp _cn = Array ("", "million "," billion "), $max _len = n, $len = strlen ($num), if ($len > $max _len) {return ' outnumber ';} $num = Str_pad ($num, N, '-', 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 = + $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;} echo CONVERT_2_CN (1111). " \ n "; Echo convert_2_cn (111111)." \ n "; Echo convert_2_cn (111111111111)." \//Add a Chinese turnThe function of the number Cn_2_num ($str) {$convert _CN = array ("0", "one", "II", "three", "Restaurant", "WU", "Lu", "Qi", "ba", "JIU"), $skip _words = Array ("Pick", "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 ') {$nu m = $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 one billion one thousand one hundred one thousand one hundred one thousand one Bai one to pick one '). "                                 \ 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 Characters and currency symbols for output:var Cn_zero = "0"; var cn_one = "one"; var cn_two = "II"; var cn_three = "three"; var Cn_f our = "cn_five"; var = "WU"; var cn_six = "Lu"; var cn_seven = "Qi"; var cn_eight = "ba"; var cn_nine = "Jiu"; var Cn_ten = "Pick"; var cn_hundred = "Bai", var cn_thousand = "thousand", var cn_ten_thousand = "Million", var cn_hundred_million = "billion"; var cn_symbol = "RMB"; var CN _dollar = "Yuan", var cn_ten_cent = "Angle", var cn_cent = "cent", var Cn_integer = "whole";//Variables:var integral; represent integral part of digit number. 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 l Arge a number to convert! "); Return "";}  Http://www.knowsky.com/Process the coversion from currency digits to characters://separate integral and decimal parts Before processing coversion:parts = Currencydigits.split ("."); if (Parts.length > 1) {integral = parts[0]; decimal = parts[1];//Cut down redundant decimal digits that is after th e 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_five, 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);d ecimals = 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 decimal Part 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_dollar;} 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 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 the character//replace (/(\.\d{2}) that is not a number in the input. +$/, "$")//matches the first from the beginning of the character. All subsequent characters, since no G mark is used,//So only match starts first time  The decimal point and the last two digits are then substituted to determine that the value is finally formatted correctly.  Replace (/^0+ ([1-9])/, "$") matches numbers starting with more than 0 with the first digit of the number minus 0 and the beginning of the match.  Replace (/^0+$/, "0") matches with 0 start and end of more than 0 for a 0 that is 0000000 input, and converted to a 0//following to determine the input for the filtered legitimate digital//alert (MS);  var txt = ms.split (".");  Alert (txt[0]); If the MS value does not exist then txt[0]= the value before the decimal point otherwise equal to Ms//regexp:/\d{4} (, |$)/Match four digits and, the collection orThe 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 replaced by txt[0] and then assigned to it//regexp:/(\d) (\d{3} (, |$))/four digits are the first digit of two sets of first digits,  After three bits and other end for every two bits//and replace first, second note, the use is very good. That is, replace 4123 with 4,123//Because this expression defaults to a greedy match so that the value is matched by a loop and then replaced by a re-match so that the//12345678 can be divided into the 123,456,78 color you want (, |$) is fine, because it omits the second match , the problem t.value = Stmp = txt[0]+ (txt.length>1? ".")  +TXT[1]: "");  The final assignment to the input box//If there is a decimal point plus the acquisition into the final number otherwise displays the replaced txt[0] Bbb.value = convertcurrency (ms-0); Convert MS to digital send to NUMBER2NUM1 to convert}

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/1029596.html www.bkjia.com true http://www.bkjia.com/PHPjc/1029596.html techarticle PHP Converts the amount of money into Chinese capital, PHP will convert the amount of money into Chinese capital Echo tochinesenumber (1234567890);//One to pick up the Hundred hundred thousand Wu Wood to pick up the land million qi thousand Ba bai gather round funct ...

  • 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.