所以我在PHP中执行以下操作:
$cost = $this->reportDataStructure['ext_cost'] / $this->reportDataStructure['quantity'];
var_dump($cost); // Outputs: float(220)
$this->reportDataStructure['cost'] = sprintf("%$.2f", $cost);
我一直在获取
Warning: sprintf(): Argument number must be greater than zero in C:'xampp'htdocs'rms'site'web'module'Report'controller'InventoryReport.controller.php on line 98
属于以下行:
$this->reportDataStructure['cost'] = sprintf("%$.2f", $cost);
但正如我们所看到的:
var_dump($cost); // Outputs: float(220)
怎么回事?
$
在printf
格式字符串中用于指定后面带有说明符的参数,例如
sprintf('%2$f %1$d', $var1, $var2);
意味着它将把CCD_ 3显示为浮点,然后把$var1
显示为整数。
您的格式字符串中有$
,但在此之前没有数字。因此该数字不大于0。
如果你解释你试图用这个格式字符串完成什么,我可以用正确的方法更新答案
如果你只是想把$
放在价格之前,它需要在%
之前,而不是之后。
$this->reportDataStructure['cost'] = sprintf("$%.2f", $cost);