<?php
/**
* Created by PhpStorm.
* User: msi
* Date: 2019/9/23
* Time: 22:07
*/
//输入要转换的金额
$moneyUnitObj = new MoneyUnit();
echo $moneyUnitObj->moneyChangeBig(880070066.135 , true);
Class MoneyUnit{
private $number;
private $unit;
/**
* 金钱转大写
* @param $money
* @param bool $numerous
* @return string
*/
public function moneyChangeBig($money , $numerous = false)
{
//不得超过万亿
if (strlen($money)>13){
echo '你咋这么牛逼,你有这么多钱???';die;
}
if ($numerous){
$this->numerousNumberAndUnit();
}else{
$this->janeNumberAndUnit();
}
mb_internal_encoding('UTF-8');
//判断是不是负数
$negativeNumber = '';
$prefixNum = substr($money , 0 ,1);
if ($prefixNum == '-'){
$negativeNumber = '负';
$money = substr($money , 1);
}
//判断有没有小数点
if (strpos($money , '.')){
$tmpMoney = explode('.' , $money);
$result = $this->integerPart($tmpMoney[0]) . $this->decimalPart($tmpMoney[1]);
}else{
$result = $this->integerPart($money);
}
return $negativeNumber . $result;
}
/**
* 计算整数部分
* @param $money
* @return string
*/
private function integerPart($money){
$result = '';
$len = strlen($money);
if ($len == 1) {
$result = $this->number[$money] . $this->unit[1];
} else {
for ($i = 0; $i < $len; $i++) {
$tmpNum = substr($money, $i, 1);
$tmpUnit = strlen(substr($money, $i));
if ($tmpNum == 0) {
if (mb_substr($result, -1) != $this->number[0]) {
if ($tmpUnit == 9 || $tmpUnit == 5){
$result .= $this->unit[$tmpUnit];
}else{
$result .= $this->number[0];
}
}
} else {
$result .= $this->number[$tmpNum] . $this->unit[$tmpUnit];
}
}
}
$tmpStr = mb_substr($result, -1);
if ($tmpStr == $this->number[0]) {
$result = mb_substr($result, 0, -1) . $this->unit[1];
} elseif ($tmpStr != $this->unit[1]) {
$result .= $this->unit[1];
}
return $result;
}
/**
* 计算小数部分
* @param $money
* @return string
*/
private function decimalPart($money){
if (empty($money)){
echo '你是傻逼吗?点后面啥也不写??';die;
}
$result = $this->number[$money[0]] . '角';
if (strlen($money) > 1){
$result .= $this->number[$money[1]] . '分';
}
return $result;
}
//简体数字和单位
private function janeNumberAndUnit(){
$this->number = [
0 => '零',
1 => '一',
2 => '二',
3 => '三',
4 => '四',
5 => '五',
6 => '六',
7 => '七',
8 => '八',
9 => '九',
10 => '十'
];
$this->unit = [
1 => '元',
2 => '十',
3 => '百',
4 => '千',
5 => '万',
6 => '十',
7 => '百',
8 => '千',
9 => '亿',
10 => '十',
11 => '百',
12 => '千',
13 => '万',
14 => '兆'
];
}
//繁体数字和单位
private function numerousNumberAndUnit(){
$this->number = [
0 => '零',
1 => '壹',
2 => '贰',
3 => '叁',
4 => '肆',
5 => '伍',
6 => '陆',
7 => '柒',
8 => '捌',
9 => '玖',
10 => '拾'
];
$this->unit = [
1 => '元',
2 => '拾',
3 => '佰',
4 => '仟',
5 => '万',
6 => '拾',
7 => '佰',
8 => '仟',
9 => '亿',
10 => '拾',
11 => '佰',
12 => '仟',
13 => '万',
14 => '兆',
];
}
}
执行结果: