From fded833675e462b5b9ecb36c4313080436b412e4 Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq,com> Date: Mon, 18 Sep 2023 17:42:03 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=94=E5=9B=9E=E9=87=91=E9=A2=9D=E6=96=87?= =?UTF-8?q?=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/lib/pinyin/PinyinNumber.php | 41 ++++++++++++++++++++++++++ app/model/ZoneGoods.php | 3 ++ 2 files changed, 44 insertions(+) diff --git a/app/common/lib/pinyin/PinyinNumber.php b/app/common/lib/pinyin/PinyinNumber.php index 583f5b3..6e20853 100644 --- a/app/common/lib/pinyin/PinyinNumber.php +++ b/app/common/lib/pinyin/PinyinNumber.php @@ -11,6 +11,10 @@ class PinyinNumber protected static $unit = ['', 'shi', 'bai', 'qian', 'wan', 'shiwan', 'baiwan', 'qianwan', 'yi']; + protected static $zh_pinyin = [0 => '零', 1 => '一', 2 => '二', 3 => '三', 4 => '四', 5 => '五', 6 => '六', 7 => '七', 8 => '八', 9 => '九']; + + protected static $zh_unit = ['', '十', '百', '千', '万', '十万', '百万', '千万', '亿']; + /** * 获取数字的拼音 * @param $number @@ -47,4 +51,41 @@ class PinyinNumber if ($strtoupper) $pinyinStr = strtoupper($pinyinStr); return $pinyinStr; } + + /** + * 获取数字的汉字 + * @param $number + * @param $suffix // 后缀 + * @return string + */ + public static function getChinese($number,$suffix = '') + { + + $number = strval(round($number)); // 将数字转换为字符串 + $length = strlen($number); + $chineseStr = ''; + + for ($i = 0; $i < $length; $i++) { + $digit = intval($number[$i]); + + if ($digit != 0) { + // 处理万以上的位数 + if ($length - $i > 4) { + $chineseStr .= self::$zh_pinyin[$digit] . self::$zh_unit[$length - $i - 1]; + // 处理万位之间的零 + if ($digit == 0 && $number[$i + 1] != 0) { + $chineseStr .= '零'; + } + } else { + $chineseStr .= self::$zh_pinyin[$digit] . self::$zh_unit[$length - $i - 1]; + } + } else { + // 处理连续的0 + if ($i == 0 || $number[$i] != 0) { + $chineseStr .= self::$zh_pinyin[$digit]; + } + } + } + return $chineseStr . $suffix; + } } diff --git a/app/model/ZoneGoods.php b/app/model/ZoneGoods.php index 071750f..a60f3fc 100644 --- a/app/model/ZoneGoods.php +++ b/app/model/ZoneGoods.php @@ -2,6 +2,8 @@ namespace app\model; +use app\common\lib\pinyin\PinyinNumber; + class ZoneGoods extends \think\Model { @@ -14,6 +16,7 @@ class ZoneGoods extends \think\Model $list = self::where($where)->field($field)->order('id desc')->select()->toArray(); foreach ($list as &$item) { if ($item['cover_image']) $item['cover_image'] = get_image_url($item['cover_image']); + $item['max_awards_amount'] = PinyinNumber::getChinese($item['max_awards_amount'],'元'); if ($is_manage) { $item['important'] = '最高中奖金额'.format_money($item['max_awards_amount']); unset($item['status']);