Browse Source

返回金额文字

master
wanghongjun 2 years ago
parent
commit
fded833675
  1. 41
      app/common/lib/pinyin/PinyinNumber.php
  2. 3
      app/model/ZoneGoods.php

41
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;
}
}

3
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']);

Loading…
Cancel
Save