金额钱 - PHP 工具类
<?php
/**
* Created by PhpStorm.
* User: peeke
* Date: 2020/4/10
* Time: 13:41
*/
namespace App\common\Helpers\tools;
class PriceHelper
{
// 税备金比例
const TAX_RATE = 5;
// 税备金金额
const RESERVR_AMOUNT = 100;
const RMB_YUN = "YUN"; //元
const RMB_JIAO = "JIAO"; //角
const RMB_FEN = "FEN"; //分
const RMB_LI = "LI"; //里,小数点后3位
const RMB_WEI = "WEI"; //微,小数点后4位
/**
* 价格存整数,1元存成100
*/
public static function get_db_price($price = 0)
{
if (empty($price)) {
$price = 0;
}
$newPrice = (string)($price * 100);
return intval($newPrice);
}
/**
* 价格存整数,1元存成100
*/
public static function get_real_price($price = 0)
{
$price = round($price / 100, 2);
//最多展示3位小数,有小数的展示出来,没有的不展示
$price = number_format($price, 2, '.', '');
$price = floatval($price); //去掉多余的0,返回浮点型
return $price;
}
/**
* 格式化价格数据
*
* @param $price 单位元
* @param int $decimals 保留几位小数
* @param string $format YUN|JIAO|FEN|LI|WEI
* @return float|string
*/
public static function get_price($price, int $decimals = 2, string $format = self::RMB_YUN)
{
switch ($format) {
case self::RMB_YUN:
$price = $price * 1.0;
break;
case self::RMB_JIAO:
$price = $price / 10.0;
break;
case self::RMB_FEN:
$price = $price / 100.0;
break;
case self::RMB_LI:
$price = $price / 1000.0;
break;
case self::RMB_WEI:
$price = $price / 10000.0;
break;
}
$price = floatval($price); //去掉多余的0,返回浮点型
$price = number_format($price, $decimals, '.', ',');
return $price;
}
/**
* 获取对应的税备金
*
* @param $price 金额 单位:分
* @param $preAmount 预存金额 单位:分
* @param $taxRate 税备金计算比例
* @return false|float|int|string
*/
public static function getTaxReserveAmount($price, $preAmount = 100, $taxRate = 5)
{
//$preAmount = self::get_db_price($preAmount);
$taxReserveAmount = $price * ($taxRate / 100) + $preAmount;
$taxReserveAmount = self::get_real_price($taxReserveAmount);
$taxReserveAmount = sprintf("%.2f", $taxReserveAmount);
return $taxReserveAmount;
}
/**
* 计算税备金
*
* @param $price 订单价格
* @param $taxRate 税备金计算比例
* @return false|float|int|string
*/
public static function get_tmp_price($price, $taxRate)
{
$tmp_tax = bcmul($price, $taxRate / 100, 2) + 100;
return self::get_price($tmp_tax);
}
/**
*数字金额转换成中文大写金额
*
* @param numeric $num 要转换的小写数字或小写字符串
* @return $numStr
**/
public static function numToRmb($num, $isNeedZheng = true)
{
$oldNum = $num;
if (!is_numeric($num)) {
return $oldNum;
}
$c1 = "零壹贰叁肆伍陆柒捌玖";
$c2 = "分角元拾佰仟万拾佰仟亿";
//精确到分后面就不要了,所以只留两个小数位
$num = round($num, 2);
//将数字转化为整数
$num = $num * 100;
if ((strlen($num) > 10) || ($num < 0.01)) {
return $oldNum;
}
$i = 0;
$c = "";
while (1) {
if ($i == 0) {
//获取最后一位数字
$n = substr($num, strlen($num) - 1, 1);
} else {
$n = $num % 10;
}
//每次将最后一位数字转化为中文
$p1 = substr($c1, 3 * $n, 3);
$p2 = substr($c2, 3 * $i, 3);
if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
$c = $p1 . $p2 . $c;
} else {
$c = $p1 . $c;
}
$i = $i + 1;
//去掉数字最后一位了
$num = $num / 10;
$num = (int)$num;
//结束循环
if ($num == 0) {
break;
}
}
$j = 0;
$slen = strlen($c);
while ($j < $slen) {
//utf8一个汉字相当3个字符
$m = substr($c, $j, 6);
//处理数字中很多0的情况,每次循环去掉一个汉字“零”
if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
$left = substr($c, 0, $j);
$right = substr($c, $j + 3);
$c = $left . $right;
$j = $j - 3;
$slen = $slen - 3;
}
$j = $j + 3;
}
//这个是为了去掉类似23.0中最后一个“零”字
if (substr($c, strlen($c) - 3, 3) == '零') {
$c = substr($c, 0, strlen($c) - 3);
}
//将处理的汉字加上“整”
$zheng = '';
if ($isNeedZheng) {
$zheng = '整';
}
if (empty($c)) {
$numStr = "零元" . $zheng;
} else {
$numStr = $c . $zheng;
}
return $numStr;
}
}