@ 2014-03-17T10:19:49Z <?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>";
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 Fatal error: Array and string offset access syntax with curly braces is no longer supported in /in/NEOAR on line 33
Process exited with code 255 . Output for 7.4.0 - 7.4.33 Deprecated: Array and string offset access syntax with curly braces is deprecated in /in/NEOAR on line 33
Deprecated: Array and string offset access syntax with curly braces is deprecated in /in/NEOAR on line 44
Warning: Cannot modify header information - headers already sent by (output started at /in/NEOAR:33) in /in/NEOAR on line 80
負玖拾億零玖點玖玖<br> Output for 4.3.0 - 4.3.11 , 4.4.0 - 4.4.9 , 5.0.0 - 5.0.5 , 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 , 5.3.0 - 5.3.29 , 5.4.0 - 5.4.45 , 5.5.0 - 5.5.38 , 5.6.0 - 5.6.40 , 7.0.0 - 7.0.33 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 負玖拾億零玖點玖玖<br> preferences:dark mode live preview
321.74 ms | 404 KiB | 460 Q