number_format中的PHP错误

时间:2014-04-18 05:13:19

标签: php number-formatting

我有这个变量:$this->{$prefix . '_total'}可以根据具体情况等于任何数字,但暂时可以说$this->{$prefix . '_total'} = 4000

所以我使用$this->{$prefix . '_total'}来获取4,000

然而,我不知道为什么,但我得到了4

其他零发生了什么?我尝试使用不同的值,例如1564.13,在所有情况下,输出只是第一个数字。所以我试着这样:

number_format(($this->{$prefix . '_total'}*100000))

它也不起作用!我仍然只得到第一个数字。为什么??这让我的思绪在很多层面上都震撼了。请帮忙。

谢谢。

全功能:

function render($indent = "", InvoicePayment $payment = null)
{
$prefix = (!is_null($payment) && !$payment->isFirst()) ? 'second' : 'first';
$tm_added = is_null($payment) ? $this->tm_added : $payment->dattm;

$newline = "\r\n";

$price_width = max(mb_strlen(Am_Currency::render($this->{$prefix . '_total'}, $this->currency)), 8);

$column_padding = 1;
$column_title_max = 60;
$column_title_min = 20;
$column_qty = 4 + $price_width;
$column_num = 3;
$column_amount = $price_width;
$space = str_repeat(' ', $column_padding);

$max_length = 0;
foreach ($this->getItems() as $item) {
    $max_length = max(mb_strlen(___($item->item_title)), $max_length);
}

$column_title = max(min($max_length, $column_title_max), $column_title_min);
$row_width = $column_num + $column_padding +
             $column_title + $column_padding +
             $column_qty + $column_padding +
             $column_amount + $column_padding;

$column_total = $column_title +
                $column_qty + $column_padding;
$total_space = str_repeat(' ', $column_padding + $column_num + $column_padding);

$border = $indent . str_repeat('-', $row_width) . "$newline";

$out = $indent . ___("Invoice") . ' #' . $this->public_id . " / " . amDate($tm_added) . "$newline";
$out .= $border;
$num = 1;
foreach ($this->getItems() as $item) {
    $title = explode("\n", $this->wordWrap(___($item->item_title), $column_title, "\n", true));
    $out .= $indent . sprintf("{$space}%{$column_num}s{$space}%-{$column_title}s{$space}%{$column_qty}s{$space}%{$price_width}s$newline",
        $num . '.', $title[0], $item->qty . 'x' . Am_Currency::render($item->{$prefix . '_price'}, $this->currency), Am_Currency::render($item->{$prefix . '_total'}, $this->currency));
    for ($i=1; $i<count($title); $i++)
        $out .= $indent . sprintf("{$space}%{$column_num}s{$space}%-{$column_title}s$newline", ' ', $title[$i]);
    $num++;
}
$out .= $border;
if ($this->{$prefix . '_subtotal'} != $this->{$prefix . '_total'})
    $out .= $indent . sprintf("{$total_space}%-{$column_total}s{$space}%{$price_width}s$newline", ___('Subtotal'), Am_Currency::render($this->{$prefix . '_subtotal'}, $this->currency));
if ($this->{$prefix . '_discount'} > 0)
    $out .= $indent . sprintf("{$total_space}%-{$column_total}s{$space}%{$price_width}s$newline", ___('Discount'), Am_Currency::render($this->{$prefix . '_discount'}, $this->currency));
if ($this->{$prefix . '_shipping'} > 0)
    $out .= $indent . sprintf("{$total_space}%-{$column_total}s{$space}%{$price_width}s$newline", ___('Shipping'), Am_Currency::render($this->{$prefix . '_shipping'}, $this->currency));
if ($this->{$prefix . '_tax'} > 0)
    $out .= $indent . sprintf("{$total_space}%-{$column_total}s{$space}%{$price_width}s$newline", ___('Tax'), Am_Currency::render(number_format($this->{$prefix . '_tax'}), $this->currency));
$out .= $indent . sprintf("{$total_space}%-{$column_total}s{$space}%{$price_width}s$newline", ___('Total'), Am_Currency::render($this->{$prefix . '_total'}, $this->currency));
$out .= $border;
if ($this->rebill_times) {
    $terms = explode("\n", $this->wordWrap(___($this->getTerms()), $row_width, "\n", true));
    foreach ($terms as $term_part)
        $out .= $indent . $term_part . $newline;
    $out .= $border;
}
return $out;
}

这是渲染功能

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);
    }

2 个答案:

答案 0 :(得分:0)

试试这个

<?php
   echo number_format($number,0,'',',');
?>

答案 1 :(得分:0)

在这里刺入黑暗来解释这种现象:

echo (int)number_format(1000);

这导致“1”。因为number_format生成“1,000”,并且(int)尝试将其解析为整数,并且因为“,”不是整数规范的有效部分,而是它停止并返回的位置只有1个}}”。请参阅http://php.net/manual/en/language.types.string.php#language.types.string.conversion

这样的事情必定发生在1