6
0
Fork 0

去除小数点后尾随零

release
李静 2021-12-15 14:14:41 +08:00
parent fee8470096
commit 70471f0c06
3 changed files with 8 additions and 10 deletions

View File

@ -8,14 +8,14 @@ class Numeric
* 去除数字字符串小数点后多余的 0
*
* @param mixed $value
* @return string
* @return mixed
*/
public static function trimZero($value)
public static function trimTrailingZero($value)
{
if (is_numeric($value) && strpos($value, '.') !== false) {
$value = rtrim(rtrim($value, '0'), '.');
$value = rtrim(rtrim($value, '0'), '.') ?: '0';
}
return $value ?: '0';
return $value;
}
}

View File

@ -62,6 +62,6 @@ class Order extends Model
*/
public function getTotalAmountFormatAttribute()
{
return Numeric::trimZero(bcdiv($this->attributes['total_amount'], 100, 2));
return Numeric::trimTrailingZero(bcdiv($this->attributes['total_amount'], 100, 2));
}
}

View File

@ -71,8 +71,6 @@ class UserCoupon extends Model
/**
* 优惠券可用范围规则
*
* @return void
*/
public function ranges()
{
@ -100,10 +98,10 @@ class UserCoupon extends Model
// 如果是折扣券
if ($this->isDiscountCoupon()) {
return NumericHelper::trimZero(bcdiv($value, 10, 1));
return NumericHelper::trimTrailingZero(bcdiv($value, 10, 1));
}
return NumericHelper::trimZero(bcdiv($value, 100, 2));
return NumericHelper::trimTrailingZero(bcdiv($value, 100, 2));
}
/**
@ -113,7 +111,7 @@ class UserCoupon extends Model
*/
public function getCouponThresholdFormatAttribute(): string
{
return NumericHelper::trimZero(
return NumericHelper::trimTrailingZero(
bcdiv($this->attributes['coupon_threshold'], 100, 2)
);
}