1.将阿拉伯数字转换为中文大写数字
<?php namespace core\components; class PriceHelper extends \yii\base\Component{ /** * @param $num * @param bool $mode * @return string * 将数字转换为中文大写数字 */ public static function num2zh($num,$mode=true){ $char = array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖"); $dw = array("","拾","佰","仟","","萬","億","兆"); $dec = "點"; $retval = ""; if($mode) { preg_match_all("/^0*(\d*)\.?(\d*)/", $num, $ar); }else { preg_match_all("/(\d*)\.?(\d*)/", $num, $ar); } if($ar[2][0] != "") $retval = $dec . self::num2zh($ar[2][0],false); //如果有小数,先递归处理小数 if($ar[1][0] != "") { $str = strrev($ar[1][0]); for($i=0;$i<strlen($str);$i++) { $out[$i] = $char[$str[$i]]; if($mode) { $out[$i] .= $str[$i] != "0"? $dw[$i % 4] : ""; if($str[$i]+$str[$i] == 0) $out[$i] = ""; if($i % 4 == 0) $out[$i] .= $dw[4+floor($i/4)]; } } $retval = join("",array_reverse($out)) . $retval; } return $retval; } }