在Laravel图表上显示2个小数点

时间:2019-11-25 04:02:08

标签: php mysql laravel phpmyadmin double

我想为我在控制器中声明的每个变量显示2个小数点

private $dailyCapital=0;
private $dailySale=0;
private $dailyProfit;

因此,我尝试在每个$this前面加倍。

(double)$this->dailySale += (double)$sale->price;
(double)$this->dailyCapital += (double)($item->buying_price * $sale->quantity);
(double)$this->dailyProfit = (double)$this->dailySale - (double)$this->dailyCapital;

将其发送到view后,

->dataset('Sales by Day', 'doughnut', [$this->dailyCapital, $this->dailySale, $this->dailyProfit])

所有三个都不返回小数点后两位。

1 个答案:

答案 0 :(得分:0)

您应该使用number_format()

(double)$this->dailySale = number_format((double)$sale->price, 2, '.', '');
(double)$this->dailyCapital = number_format((double)($item->buying_price * $sale->quantity), 2, '.', '');
(double)$this->dailyProfit = number_format((double)$this->dailySale - (double)$this->dailyCapital, 2, '.', '');

此外,除非您的$this->dailySale$this->dailyCapital等变量在声明后分配了数值,否则应使用=而不是+=没有为其添加一些值的初始数值。