我有这个变量:
$out .= $indent . sprintf("{$total_space}%-{$column_total}s{$space}%{$price_width}s$newline", ___('Tax'), Am_Currency::render($this->{$prefix . '_tax'}, $this->currency));
暂时等于4000.我需要将其格式化为:4,000所以我改变了我的代码如下:
$out .= $indent . sprintf("{$total_space}%-{$column_total}s{$space}%{$price_width}s$newline", ___('Tax'), Am_Currency::render(number_format($this->{$prefix . '_tax'}), $this->currency));
然而,我不知道为什么,但我得到了4。其他零怎么了?我尝试使用不同的值,例如1214.92,并且在所有情况下输出都省略了最后3位数。
这是渲染功能:
static function render($value, $currency = null, $locale = null)
{
return (string)self::create($value, $currency, $locale);
}
public function __toString()
{
$format = array_key_exists($this->currency, $this->formats) ?
$this->formats[$this->currency] :
'%.2f %s';
return sprintf($format, $this->value, $this->currency);
}
我甚至尝试过改变上面的代码:
return sprintf($format, number_format($this->value), $this->currency);
但它也没有用。你能告诉我什么错了吗?这让我大吃一惊!
谢谢。
答案 0 :(得分:0)
您的sprintf
格式覆盖了您的number_format
功能...同时使用这两种格式没有任何意义(至少不是字符串中的相同数字) 。如果您单独使用number_format
,则会看到它正确格式化。
我建议使用money_format
功能,例如:
//money format
setlocale(LC_MONETARY, 'en_US');
...
return money_format('%.2n', $this->value);