金額錢 - 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;
}
}