3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php /** * 数字转中文 * 数字在999999999999以下 * 数字够大的时候小数点后会取整,奇怪了 * @param string $number 无序数组 * @return string $str 有序数组 */ function number2cn($number) { if(!is_numeric($number)) { return "非数字"; } elseif($number > 999999999999 || $number < -999999999999) { return "数字超出范围"; } $p = explode(".",$number); $cn_arr = array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖"); $p_arr = array("","拾","佰","仟"); $unit_arr = array("","萬","億"); $str = ""; $zero = false;//零标志 $unit = "";//单位 //处理小数点后数字 if(isset($p[1])) {//小数点后有数字存在 $dot = "點"; $l = strlen($p[1]); for($i=0;$i<$l;$i++) { $dot .= $cn_arr[$p[1]{$i}]; } } else { $dot = ""; } $number = $p[0]; //小数点前数字 $l = strlen($number);//数字位数 for($i=$l-1,$j=0;$i>=0;$i--,$j++) { $pos = $number{$i}; if($pos=="-") { $str = "負".$str; break; } if($j%4==0) { $unit = $unit_arr[$j/4]; } if($pos == '0') { $zero = true; continue; } if($zero) { if($str != "") { $str = $cn_arr[$pos].$p_arr[$j%4].$unit.$cn_arr[0].$str; } else { $str = $cn_arr[$pos].$p_arr[$j%4].$unit; } $zero = false; } else { $str = $cn_arr[$pos].$p_arr[$j%4].$unit.$str; } $unit = ""; } return $str.$dot; } //示例 header('Content-Type: text/html; charset=utf-8'); $n = -9000000009.99; echo number2cn($n)."<br>";
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
負玖拾億零玖點玖玖<br>