search
Homephp教程php手册一个数字转换为汉字的函数

可以实现数字转换为金额汉字或者普通的汉字表示:
/**<br>  * 数字转换为中文<br>  * @param  string|integer|float  $num  目标数字<br>  * @param  integer $mode 模式[true:金额(默认),false:普通数字表示]<br>  * @param  boolean $sim 使用小写(默认)<br>  * @return string<br>  */<br> function number2chinese($num,$mode = true,$sim = true){<br>     if(!is_numeric($num)) return '含有非数字非小数点字符!';<br>     $char    = $sim ? array('零','一','二','三','四','五','六','七','八','九')<br>                 : array('零','壹','贰','叁','肆','伍','陆','柒','捌','玖');<br>     $unit    = $sim ? array('','十','百','千','','万','亿','兆')             <br>                 : array('','拾','佰','仟','','萬','億','兆');<br>     $retval  = $mode ? '元':'点';<br>  <br>     //小数部分<br>     if(strpos($num, '.')){<br>         list($num,$dec) = explode('.', $num);<br>         $dec = strval(round($dec,2));<br>         if($mode){<br>             $retval .= "{$char[$dec['0']]}角{$char[$dec['1']]}分";<br>         }else{<br>             for($i = 0,$c = strlen($dec);$i                  $retval .= $char[$dec[$i]];<br>             }<br>         }<br>     }<br>     //整数部分<br>     $str = $mode ? strrev(intval($num)) : strrev($num);<br>     for($i = 0,$c = strlen($str);$i          $out[$i] = $char[$str[$i]];<br>         if($mode){<br>             $out[$i] .= $str[$i] != '0'? $unit[$i%4] : '';<br>             if($i>1 and $str[$i]+$str[$i-1] == 0){<br>                 $out[$i] = '';<br>             }<br>             if($i%4 == 0){<br>                 $out[$i] .= $unit[4+floor($i/4)];<br>             }<br>         }<br>     }<br>     $retval = join('',array_reverse($out)) . $retval;<br>     return $retval;<br> }<br> $num = '0123648867.789';<br> echo $num,'';<br> //普通数字的汉字表示<br> echo '普通:',number2chinese($num,false),'';<br> //金额汉字表示<br> echo '金额(简体):',number2chinese($num,true),'';<br> echo '金额(繁体):',number2chinese($num,true,false);

AD:真正免费,域名+虚机+企业邮箱=0元

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment