您好,欢迎访问一九零五行业门户网

php将金额数字转化为中文大写_php技巧

php将金额数字转化为中文大写
echo tochinesenumber(1234567890);//壹拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾圆function tochinesenumber($money){ $money = round($money,2); $cnynums = array(零,壹,贰,叁,肆,伍,陆,柒,捌,玖); $cnyunits = array(圆,角,分); $cnygrees = array(拾,佰,仟,万,拾,佰,仟,亿); 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; }
代码二:
/***数字金额转换成中文大写金额的函数*string int $num 要转换的小写数字或小写字符串*return 大写字母*小数位为两位**/function num_to_rmb($num){ $c1 = 零壹贰叁肆伍陆柒捌玖; $c2 = 分角元拾佰仟万拾佰仟亿; //精确到分后面就不要了,所以只留两个小数位 $num = round($num, 2); //将数字转化为整数 $num = $num * 100; if (strlen($num) > 10) { return 金额太大,请检查; } $i = 0; $c = ; while (1) { if ($i == 0) { //获取最后一位数字 $n = substr($num, strlen($num)-1, 1); } else { $n = $num % 10; } //每次将最后一位数字转化为中文 $p1 = substr($c1, 3 * $n, 3); $p2 = substr($c2, 3 * $i, 3); if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) { $c = $p1 . $p2 . $c; } else { $c = $p1 . $c; } $i = $i + 1; //去掉数字最后一位了 $num = $num / 10; $num = (int)$num; //结束循环 if ($num == 0) { break; } } $j = 0; $slen = strlen($c); while ($j 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('万','亿','-'),$str);$str = preg_replace(/-{2,}/,,$str);$str = str_replace(array('零','-'),array('','零'),$str);return $str;}echo convert_2_cn(1111).\n;echo convert_2_cn(111111).\n;echo convert_2_cn(111111111111).\n;//补充一个中文转数字的function cn_2_num($str){$convert_cn = array(零,壹,贰,叁,肆,伍,陆,柒,捌,玖);$skip_words = array(拾,佰,仟);$str = str_replace($skip_words,,$str);$len = mb_strlen($str,'utf-8');$num = 0;$k = '';for($i=0;$i
代码四:
function convertcurrency(currencydigits) {// constants:var maximum_number = 99999999999.99;// predefine the radix characters and currency symbols for output:var cn_zero = 零;var cn_one = 壹;var cn_two = 贰;var cn_three = 叁;var cn_four = 肆;var cn_five = 伍;var cn_six = 陆;var cn_seven = 柒;var cn_eight = 捌;var cn_nine = 玖;var cn_ten = 拾;var cn_hundred = 佰;var cn_thousand = 仟;var cn_ten_thousand = 万;var cn_hundred_million = 亿;var cn_symbol = 人民币;var cn_dollar = 元;var cn_ten_cent = 角;var cn_cent = 分;var cn_integer = 整;// 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 large 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 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_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);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 0) { outputcharacters += digits[0]; } zerocount = 0; outputcharacters += digits[number(d)] + radices[modulus]; } if (modulus == 0 && zerocount 1?.+txt[1]:); //最终赋值到输入框中 //如果有小数点则加上并购成最终数字否则显示替换后的txt[0] bbb.value = convertcurrency(ms-0); //将ms转换为数字送到number2num1去转换}
以上所述就是本文的全部内容了,希望大家能够喜欢。
其它类似信息

推荐信息