PHP
·
发表于 5年以前
·
阅读量:8285
Ö±½ÓÉÏʵÀý£¬Ð´µ½ ǧÒÚÉÏÁË¡£
/**
* @author jaËÌ
* °ÑÊý×Ö1-1ÒÚ»»³Éºº×Ö±iÊo£¬È磺123->Ò»°Ù¶þÊ®Èý
* @param [num] $num [Êý×Ö]
* @return [string] [string]
*/
function numToWord($num)
{
$chiNum = array('Áa', 'Ò»', '¶þ', 'Èý', 'ËÄ', 'Îa', 'Áu', 'Æß', '°Ë', '¾Å');
$chiUni = array('','Ê®', '°Ù', 'ǧ', 'Ío', 'ÒÚ', 'Ê®', '°Ù', 'ǧ');
$chiStr = '';
$num_str = (string)$num;
$count = strlen($num_str);
$last_flag = true; //ÉÏÒ»¸o ÊÇ*ñΪ0
$zero_flag = true; //ÊÇ*ñµÚÒ»¸o
$temp_num = null; //ÁÙʱÊý×Ö
$chiStr = '';//Æ´½Ó½a¹u
if ($count == 2) {//Á½Î»Êý
$temp_num = $num_str[0];
$chiStr = $temp_num == 1 ? $chiUni[1] : $chiNum[$temp_num].$chiUni[1];
$temp_num = $num_str[1];
$chiStr .= $temp_num == 0 ? '' : $chiNum[$temp_num];
}else if($count > 2){
$index = 0;
for ($i=$count-1; $i >= 0 ; $i--) {
$temp_num = $num_str[$i];
if ($temp_num == 0) {
if (!$zero_flag && !$last_flag ) {
$chiStr = $chiNum[$temp_num]. $chiStr;
$last_flag = true;
}
}else{
$chiStr = $chiNum[$temp_num].$chiUni[$index%9] .$chiStr;
$zero_flag = false;
$last_flag = false;
}
$index ++;
}
}else{
$chiStr = $chiNum[$num_str[0]];
}
return $chiStr;
}
$num = 150;
echo numToWord($num);
½¨¶þ£º
<?php
/**
* Êý×Öת»»ÎªÖÐÎÄ
* @param string|integer|float $num Ä¿±eÊý×Ö
* @param integer $mode ģʽ[true:½ð¶i£¨Ä¬ÈÏ£(C),false:ÆÕͨÊý×Ö±iʾ]
* @param boolean $sim ʹÓÃСд£¨Ä¬ÈÏ£(C)
* @return string
*/
function number2chinese($num,$mode = true,$sim = true){
if(!is_numeric($num)) return 'º¬ÓÐ*ÇÊý×Ö*ÇСÊýµa×Ö*u£¡';
$char = $sim ? array('Áa','Ò»','¶þ','Èý','ËÄ','Îa','Áu','Æß','°Ë','¾Å')
: array('Áa','Ò¼','*¡','Èþ','ËÁ','Îe','½','Æa','°Æ','¾Á');
$unit = $sim ? array('','Ê®','°Ù','ǧ','','Ío','ÒÚ','Õ×')
: array('','Ê°','°Û','Ǫ','','Èf','ƒ|','Õ×');
$retval = $mode ? 'Ôª':'µa';
//СÊý²¿*Ö
if(strpos($num, '.')){
list($num,$dec) = explode('.', $num);
$dec = strval(round($dec,2));
if($mode){
$retval .= "{$char[$dec['0']]}½Ç{$char[$dec['1']]}*Ö";
}else{
for($i = 0,$c = strlen($dec);$i < $c;$i++) {
$retval .= $char[$dec[$i]];
}
}
}
//ÕuÊý²¿*Ö
$str = $mode ? strrev(intval($num)) : strrev($num);
for($i = 0,$c = strlen($str);$i < $c;$i++) {
$out[$i] = $char[$str[$i]];
if($mode){
$out[$i] .= $str[$i] != '0'? $unit[$i%4] : '';
if($i>1 and $str[$i]+$str[$i-1] == 0){
$out[$i] = '';
}
if($i%4 == 0){
$out[$i] .= $unit[4+floor($i/4)];
}
}
}
$retval = join('',array_reverse($out)) . $retval;
return $retval;
}
//ʵÀýµ÷ÓÃ=====================================================
$num = '0123648867.789';
echo $num,'<br>';
//ÆÕͨÊý×ֵĺº×Ö±iʾ
echo 'ÆÕͨ:',number2chinese($num,false),'';
echo '<br>';
//½ð¶iºº×Ö±iʾ
echo '½ð¶i(¼oÌa):',number2chinese($num,true),'';
echo '<br>';
echo '½ð¶i(*±Ìa):',number2chinese($num,true,false);
ÒÔÉÏËuÊo¾ÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝÁË£¬Ï£Íu´o¼ÒÄܹ»Ï²»¶¡£