/**
* 对价格进行向上或向下取整
* @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);
}
对价格进行向上或向下取整
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 观其大纲 page 01 基础知识 1 MySQL数据库概要 2 简单MySQL环境 3 数据的存储和获取 4 M...
- number++和 ++number 的区别 单独使用number++和++number,都是让自变量自增加一,如...
- 以下为本人当年初学MySQL时做的笔记,也从那时起没再更新过,但还是囊括了基本的知识点,有时还翻出来查查。是不是干...