Last active
July 19, 2016 04:02
-
-
Save jae-jae/d25fbf7f6d67a8e6354df3d3e767a9de to your computer and use it in GitHub Desktop.
PHP代码片段
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 把金额转换成人民币大写 | |
*/ | |
function getCapitalCny($price) | |
{ | |
if($price > 999999999999999) { | |
throw new ErrorException('Amount out of range'); | |
} | |
$numbers = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']; | |
$units_integer = ['元', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟', '万', '拾', '佰', '仟']; | |
$units_decimal = ['角', '分']; | |
$cny = []; | |
$_n = 0; | |
list($integers, $decimals) = explode('.', number_format($price, 2, '.', '')); | |
foreach(array_reverse(str_split($integers)) as $i => $n) { | |
if($i > 0 && !($i % 4) && in_array($cny[0], $units_integer)) { | |
array_shift($cny); | |
} | |
$_cny = $n > 0 || (!($i % 4) && $integers) ? ($n > 0 ? $n : null) . $units_integer[$i] : (!$_n && !$n ? null : $n); | |
if($_cny !== null) { | |
array_unshift($cny, $_cny); | |
} | |
$_n = $n; | |
} | |
if($decimals > 0) { | |
foreach(str_split($decimals) as $i => $n) { | |
if($n > 0) { | |
array_push($cny, $n . $units_decimal[$i]); | |
} | |
} | |
} else { | |
if($integers == 0) { | |
array_push($cny, $numbers[0] . $units_integer[0]); | |
} | |
array_push($cny, '整'); | |
} | |
return str_replace(array_keys($numbers), $numbers, implode('', $cny)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PHP代码片段 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment