Laravel:遇到格式不正确的数值(计算中)

时间:2018-06-27 16:49:51

标签: php laravel math

当前,我正在跟踪https://github.com/drehimself/laravel-ecommerce-example,并且一直遇到以下错误:

遇到格式不正确的数值

这次是关于我的Coupon模型第18行:

  

namespace App;

use Illuminate\Database\Eloquent\Model;

class Coupon extends Model { 
    public static function findByCode($code)
    {
        return self::where('code', $code)->first();
    }
    public function discount($total)
    {
        if ($this->type == 'fixed') {
            return $this->value;
        } elseif ($this->type == 'percent') {
            return round(($this->percent_off / 100) * $total);
        } else {
            return 0;
        }
    } 
}

由于该原因,我无法显示结果也无法加载页面..有人可以帮我吗?预先谢谢你!

1 个答案:

答案 0 :(得分:0)

您可以将其转换为整数,但是我个人会发现$this->percent_off的值等于...,但是以下方法应该可以...

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Coupon extends Model { 
    public static function findByCode($code)
    {
        return self::where('code', $code)->first();
    }
    public function discount($total)
    {
        if ($this->type == 'fixed') {
            return $this->value;
        } elseif ($this->type == 'percent') {
            return round(((int)$this->percent_off / 100) * $total);
        } else {
            return 0;
        }
    } 
}