caipan_shop_admin/app/service/Math.php

61 lines
1.3 KiB
PHP
Executable File

<?php
namespace app\service;
/**
* 精准计算 依赖bc库
*
* Class Math
* @package app\service
*/
class Math
{
// 加法
public static function add(string $num1, string $num2, $scale = 2): string
{
return bcadd($num1, $num2, $scale);
}
// 减法
public static function sub(string $num1, string $num2, $scale = 2): string
{
return bcsub($num1, $num2, $scale);
}
// 乘法
public static function mul(string $num1, string $num2, $scale = 2): string
{
return bcmul($num1, $num2, $scale);
}
// 除法
public static function div(string $num1, string $num2, $scale = 2): string
{
return bcdiv($num1, $num2, $scale);
}
// 求余、取模
public static function mod(string $num1, string $num2, $scale = 2): string
{
return bcmod($num1, $num2, $scale);
}
// 分转元
public static function fen2Yuan(int $amount): string
{
return self::div($amount, '100');
}
// 元转分
public static function yuan2Fen(string $amount, int $scale = 0): string
{
return self::mul($amount, '100', $scale);
}
// 格式化 默认保留小数点后两位
public static function format($amount, int $scale = 2): string
{
return number_format($amount, $scale, '.', '');
}
}