PHP将阿拉伯数字转化为汉字的方法

WBOY
Release: 2016-06-20 13:05:00
Original
1136 people have browsed it

有木有遇到财务相关的开发,好端端的阿拉伯数字,非得整成汉字形式显示,这也就罢了,还得整成繁体汉字。很苦恼吧。

下面将和你分享一下本人遇到需要利用PHP将阿拉伯数字转化为汉字时的处理办法,可以总结为一个函数,具体自己研究去吧。

<p>function num2char($num,$mode=true){</p>	$char = array('零','一','二','三','四','五','六','七','八','九');<br />	//$char = array('零','壹','贰','叁','肆','伍','陆','柒','捌','玖);<br />	$dw = array('','十','百','千','','万','亿','兆');<br />	//$dw = array('','拾','佰','仟','','萬','億','兆');<br />	$dec = '点';  //$dec = '點';<br />	$retval = '';<br />	if($mode){<br />		preg_match_all('/^0*(\d*)\.?(\d*)/',$num, $ar);<br />	}else{<br />		preg_match_all('/(\d*)\.?(\d*)/',$num, $ar);<br />	}<br />	if($ar[2][0] != ''){<br />		$retval = $dec . ch_num($ar[2][0],false); //如果有小数,先递归处理小数<br />	}<br />	if($ar[1][0] != ''){<br />		$str = strrev($ar[1][0]);<br />		for($i=0;$i<strlen($str);$i++) {<br />			$out[$i] = $char[$str[$i]];<br />			if($mode){<br />				$out[$i] .= $str[$i] != '0'? $dw[$i%4] : '';<br />				if($str[$i]+$str[$i-1] == 0){<br />					$out[$i] = '';<br />				}<br />				if($i%4 == 0){<br />					$out[$i] .= $dw[4+floor($i/4)];<br />				}<br />			}<br />		}<br />		$retval = join('',array_reverse($out)) . $retval;<br />	}<br />	return $retval;<br /><p>}</p>
Copy after login

使用示例:

echo '300045.0123:'.num2char('300045.0123');//www.scutephp.com


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template