大写中文 = array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖"), 单位 = array("拾","佰","仟","万","拾","佰","仟","亿");

要求将数字转换成 大写中文,并且可以将大写中文转回数字

2010-12-31 04:34:47

7 Answers

我提供一个我经收藏的PHP的一个的币制大小写转换程序
function cny($ns) { static $cnums=array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖"), $cnyunits=array("圆","角","分"), $grees=array("拾","佰","仟","万","拾","佰","仟","亿"); list($ns1,$ns2)=explode(".",$ns,2); $ns2=array_filter(array($ns2[1],$ns2[0])); $ret=array_merge($ns2,array(implode("", _cny_map_unit(str_split($ns1), $grees)), "")); $ret=implode("",array_reverse(_cny_map_unit($ret,$cnyunits))); return str_replace(array_keys($cnums), $cnums,$ret); } function _cny_map_unit($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; }
2010-12-31 06:49:33

我来一个数字转中文不限制长度的吧:

<?php function num2ch($num) { if($num == '0') return '零'; $chs = array('0','壹','贰','叁','肆','伍','陆','柒','捌','玖'); $uni = array('','拾','佰','仟'); $exp = array('','万'); $num.= ''; $res = ''; for($i=strlen($num)-1,$k=0;$i>=0;$k++) { $str = ''; //按照中文读写习惯,每4个字为一段进行转化 for($j=0;$j<4 && $i>=0;$j++,$i--) { $u = $num{$i}>0? $uni[$j] : '';//非0的数字后面添加单位 $str = $chs[$num{$i}].$u.$str; } $str = preg_replace("/0+$/","",$str);//去掉末尾的0 $str = preg_replace("/0+/","零",$str);//替换0 if( !isset($exp[$k]) ) $exp[$k] = $exp[$k-2].'亿';//构建单位 $u2 = $str!=''? $exp[$k] : ''; $res = $str.$u2.$res; } return $res; } print_r(num2ch('2000001050600090'));

2010-12-31 08:34:28
<?php //先贴一个数字转中文的,最多12位数 function convert_2_cn($num) { $convert_cn = array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖"); $repair_number = array('零仟零佰零拾零','万万','零仟','零佰','零拾'); $unit_cn = array("拾","佰","仟","万","亿"); $exp_cn = array("","万","亿"); $max_len = 12; $len = strlen($num); if($len > $max_len) { return 'outnumber'; } $num = str_pad($num,12,'-',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('万','亿','-'),$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<$len;$i++) { $cn = mb_substr($str,$i,1,'utf-8'); if($cn == '亿') { $num = $num + intval($k)*100000000; $k = ''; } elseif($cn == '万') { $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('壹仟壹佰壹拾壹亿壹仟壹佰壹拾壹万壹仟壹佰壹拾壹')."\n"; echo cn_2_num('拾壹万壹仟壹佰壹拾壹')."\n"; ?>
2010-12-31 09:46:29
/** * 人民币数字转中文币制 * @param string $data 待转换的数字 * @return string 返回转换后的中文数字 */ function numberTocncap($data){ $capnum = array( "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" ); $capdigit = array( "", "拾", "佰", "仟" ); $subdata = explode( ".", $data ); $yuan = $subdata[0]; $j = 0; $nonzero = 0; for( $i=0; $i<strlen($subdata[0]); $i++ ){ if( $i==0 ){ //确定个位 if($subdata[1]){ $cncap = (substr($subdata[0],-1,1)!=0) ? "元" : "元零"; }else{ $cncap = "元"; } } if( $i==4 ){ //确定万位 $j = 0; $nonzero = 0; $cncap = "万" . $cncap; } if($i==8){ //确定亿位 $j = 0; $nonzero = 0; $cncap = "亿" . $cncap; } $numb = substr($yuan,-1,1); //截取尾数 $cncap = ($numb) ? $capnum[$numb].$capdigit[$j].$cncap : (($nonzero)?"零".$cncap:$cncap); $nonzero = ($numb) ? 1 : $nonzero; $yuan = substr($yuan,0,strlen($yuan)-1); //截去尾数 $j++; } if($subdata[1]){ $chiao=(substr($subdata[1],0,1))?$capnum[substr($subdata[1],0,1)]."角":"零"; $cent=(substr($subdata[1],1,1))?$capnum[substr($subdata[1],1,1)]."分":"零分"; } $cncap .= $chiao.$cent."整"; $cncap = preg_replace("/(零)+/","\\1",$cncap); //合并连续“零” return $cncap; }
2010-12-31 11:27:13
function numToCny($num){ $capUnit=array('万','亿','万','圆',''); $capDigit=array(2=>array('角','分',''), 4=>array('仟','佰','拾','')); $capNum=array('零','壹','贰','叁','肆','伍','陆','柒','捌','玖'); if ((strpos(strval($num),'.')>16)||(!is_numeric($num))) return ''; $num = sprintf("%019.2f",$num); $CurChr=array('',''); for ($i=0,$ret='',$j=0;$i<5;$i++,$j=$i*4+floor($i/4)){ $nodeNum=substr($num,$j,4); for($k=0,$subret='',$len=strlen($nodeNum);(($k<$len)&&(intval(substr($nodeNum,$k))!=0));$k++){ $CurChr[$k%2] = $capNum[$nodeNum{$k}].(($nodeNum{$k}==0)?'':$capDigit[$len][$k]); if (!(($CurChr[0]==$CurChr[1]) && ($CurChr[$k%2]==$capNum[0]))) if(!(($CurChr[$k%2] == $capNum[0]) && ($subret=='') && ($ret==''))) $subret .= $CurChr[$k%2]; } $subChr = $subret.(($subret=='')?'':$capUnit[$i]); if(!(($subChr == $capNum[0]) && ($ret==''))) $ret .= $subChr; } $ret=($ret=="")?$capNum[0].$capUnit[3]:$ret; return $ret; }
2010-12-31 12:28:07
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 < 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;//如果等于上次输入则返回 var ms = t.value.replace(/[^\d\.]/g,"").replace(/(\.\d{2}).+$/,"$1").replace(/^0+([1-9])/,"$1").replace(/^0+$/,"0"); //replace(/[^\d\.]/g,"")去掉输入当中不是数字和.的字符 //replace(/(\.\d{2}).+$/,"$1") //匹配从字符开始的第一个.后面的所有字符,由于没有使用g标记, //所以只匹配开始第一次 然后用小数点和后两位进行替换以确定数值最后的格式正确 高. //replace(/^0+([1-9])/,"$1") 匹配以多个0开头的数值替换为去掉0后的数值做为数字的第一位 也是匹配开始的一次. //replace(/^0+$/,"0") 匹配以0开始和结束的多个0为一个0 也就是0000000 输入->转换成一个0 //以下确定输入的为过滤后的合法数字 //alert(ms); var txt = ms.split("."); //alert(txt[0]); //如果ms值不小数点存在则txt[0]=小数点前的值否则等于ms //regexp:/\d{4}(,|$)/ 匹配四位数字和,的集合或者四位数字和字符结尾的集合 while(/\d{4}(,|$)/.test(txt[0]))//如果为txt[0]=4123 txt[0] = txt[0].replace(/(\d)(\d{3}(,|$))/,"$1,$2"); //txt[0].replace(/(\d)(\d{3}(,|$))/,"$1,$2")是将txt[0]进行替换后再赋给它 //regexp:/(\d)(\d{3}(,|$))/ 将四个数字份为两组第一个数字为第一位,后三位和其他结尾为每二位 //并替换成 第一位,第二位 注意 ,的使用很好. 也就是将4123先替换成4,123 //由于此表达式默认采用贪婪匹配所以从数值后向前匹配再通过循环进行再匹配替换从而可以将 //12345678分成你想要的123,456,78 彩用(,|$)很精典,因为它略去了第二次匹配时的,问题 t.value = stmp = txt[0]+(txt.length>1?"."+txt[1]:""); //最终赋值到输入框中 //如果有小数点则加上并购成最终数字否则显示替换后的txt[0] bbb.value = convertCurrency(ms-0); //将ms转换为数字送到number2num1去转换 }
2010-12-31 14:22:48
您不能回答该问题或者回答已经关闭!

相关文章推荐

  • C#中using指令的几种用法

    using + 命名空间名字,这样可以在程序中直接用命令空间中的类型,而不必指定类型的详细命名空间,类似于Java的import,这个功能也是最常用的,几乎每个cs的程序都会用到

  • C#实例解析适配器设计模式

    将一个类的接口变成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够一起工作

  • 使用托管C++粘合C#和C++代码(二)

    本文实现一下C++代码调用C#代码的过程。我构造一个简单并且直观的例子:通过C++ UI 触发C# UI.

  • C#开发高性能Log Help类设计开发

    项目中要在操作数据库的异常处理中加入写Log日志,对于商业上有要求,写log时对其它操作尽可能影响小,不能因为加入log导致耗时太多

  • Async和Await使异步编程更简单

    C#5.0中async和await两个关键字,这两个关键字简化了异步编程,之所以简化了,还是因为编译器给我们做了更多的工作

  • C#开发中的反射机制

    反射的定义:审查元数据并收集关于它的类型信息的能力。元数据(编译以后的最基本数据单元)就是一大堆的表,当编译程序集或者模块时,编译器会创建一个类定义表,一个字段定义表,和一个方法定义表等

  • C#运行时相互关系

    C#运行时相互关系,包括运行时类型、对象、线程栈和托管堆之间的相互关系,静态方法、实例方法和虚方法的区别等等

  • C#协变和逆变

    “协变”是指能够使用与原始指定的派生类型相比,派生程度更大的类型,“逆变”则是指能够使用派生程度更小的类型

  • C#基础概念之延迟加载

    延迟加载(lazy load)是Hibernate3关联关系对象默认的加载方式,延迟加载机制是为了避免一些无谓的性能开销而提出来的,所谓延迟加载就是当在真正需要数据的时候,才真正执行数据加载操作

  • C#中的索引器的简单理解和用法

    C#中的类成员可以是任意类型,包括数组和集合。当一个类包含了数组和集合成员时,索引器将大大简化对数组或集合成员的存取操作

  • 使用托管C++粘合C#和C++代码(一)

    C#在xml读写,数据库操纵,界面构造等很多方面性能卓越;C++的效率高,是底层开发的必备武器

  • 深入C# 序列化(Serialize)、反序列化(Deserialize)

    C#中的序列化和反序列化,序列化是.NET运行时环境用来支持用户定义类型的流化的机制