/**
* 对价格进行向上或向下取整
* @param $price 价格
* @param $decimal 保留小数位数
* @param $type 1:向上 2:向下
* @return string|null
*/
public function floatPrice($price, $decimal = 2, $type = 1)
{
$tmpPrice = explode('.', $price);
$dcmnum = $tmpPrice[1] ?? 0;
$subnum = 0;
if ($dcmnum > 0) {
$subnum = bcsub(strlen($dcmnum), $decimal, 10);
}
$powint = bcpow(10, $decimal);
$tmpPrice = bcmul($price, $powint, $subnum);
$tmpPriceArr = explode('.', $tmpPrice);
$tmpPrice = $tmpPriceArr[0];
$dcm = $tmpPriceArr[1] ?? 0;
if ($dcm > 0) {
if ($type == 1 && $tmpPrice > 0) {
$tmpPrice = $tmpPrice + 1;
} elseif ($type == 2 && $tmpPrice < 0) {
$tmpPrice = $tmpPrice - 1;
}
}
return bcdiv($tmpPrice, $powint, $decimal);
}
网友评论