修复人民币转大写错误的问题

This commit is contained in:
HDM58\hdm58 2023-10-09 17:56:55 +08:00
parent 6a4bdd9bd5
commit 8e2942f1be
2 changed files with 64 additions and 27 deletions

View File

@ -1203,35 +1203,72 @@ function table_assign($code = 0, $msg = '请求成功', $data = [], $httpCode =
* 人民币转大写 * 人民币转大写
* @param * @param
*/ */
function cny($ns) function cny($amount){
{ $capitalNumbers = [
static $cnums = array("", "", "", "", "", "", "", "", "", ""), '零',
$cnyunits = array("", "", ""), '壹',
$grees = array("", "", "", "", "", "", "", "亿"); '贰',
list($ns1, $ns2) = explode(".", $ns, 2); '叁',
$ns2 = array_filter(array($ns2[1], $ns2[0])); '肆',
$ret = array_merge($ns2, array(implode("", _cny_map_unit(str_split($ns1), $grees)), "")); '伍',
$ret = implode("", array_reverse(_cny_map_unit($ret, $cnyunits))); '陆',
return str_replace(array_keys($cnums), $cnums, $ret); '柒',
} '捌',
'玖',
];
function _cny_map_unit($list, $units) $integerUnits = ['', '拾', '佰', '仟',];
{ $placeUnits = ['', '万', '亿', '兆',];
$ul = count($units); $decimalUnits = ['角', '分', '厘', '毫',];
$xs = array(); $result = [];
foreach (array_reverse($list) as $x) { $arr = explode('.', (string)$amount);
$l = count($xs); $integer = trim($arr[0] ?? '', '-');
if ($x != "0" || !($l % 4)) { $decimal = $arr[1] ?? '';
$n = ($x == '0' ? '' : $x) . ($units[($l - 1) % $ul]); if (!((int)$decimal)) {
} else { $decimal = '';
$n = is_numeric($xs[0][0]) ? $x : '';
} }
array_unshift($xs, $n); // 转换整数部分,从个位开始
$integerNumbers = $integer ? array_reverse(str_split($integer)) : [];
$last = null;
foreach (array_chunk($integerNumbers, 4) as $chunkKey => $chunk) {
if (!((int)implode('', $chunk))) {
// 全是 0 则直接跳过
continue;
} }
return $xs; array_unshift($result, $placeUnits[$chunkKey]);
foreach ($chunk as $key => $number) {
// 去除重复 零,以及第一位的 零类似1002、110
if (!$number && (!$last || $key === 0)) {
$last = $number;
continue;
}
$last = $number;
// 类似 1022中间的 0 是不需要 佰 的
if ($number) {
array_unshift($result, $integerUnits[$key]);
}
array_unshift($result, $capitalNumbers[$number]);
}
}
if (!$result) {
$result[] = $capitalNumbers[0];
}
$result[] = '圆';
if (!$decimal) {
$result[] = '整';
}
// 转换小数位
$decimalNumbers = $decimal ? str_split($decimal) : [];
foreach ($decimalNumbers as $key => $number) {
$result[] = $capitalNumbers[$number];
$result[] = $decimalUnits[$key];
}
if (strpos((string)$amount, '-') === 0) {
array_unshift($result, '负');
}
return implode('', $result);
} }
/** /**
* 金额展示规则,超过1万时以万为单位低于1万时以千为单位低于1千时以元为单位 * 金额展示规则,超过1万时以万为单位低于1万时以千为单位低于1千时以元为单位
* @param string $money 金额 * @param string $money 金额

View File

@ -15,10 +15,10 @@ if (empty(file_exists(__DIR__ . '/../vendor/autoload.php'))) {
require __DIR__ . '/../vendor/autoload.php'; require __DIR__ . '/../vendor/autoload.php';
// 定义当前版本号 // 定义当前版本号
define('CMS_VERSION','4.09.10'); define('CMS_VERSION','4.92.10');
// 定义Layui版本号 // 定义Layui版本号
define('LAYUI_VERSION','2.8.16'); define('LAYUI_VERSION','2.8.17');
// 定义项目目录 // 定义项目目录
define('CMS_ROOT', __DIR__ . '/../'); define('CMS_ROOT', __DIR__ . '/../');