我想为我在控制器中声明的每个变量显示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])
这三个都不返回小数点后两位。
最佳答案
您应该使用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
等变量分配了数值,否则应使用=
而不是+=
,因为没有初始数值可为其添加一些值。关于php - 在Laravel图表上显示2个小数点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59025083/