繁体   English   中英

PHP错误,格式为number_format

[英]PHP Error in number_format

我有这个变量: $this->{$prefix . '_total'} $this->{$prefix . '_total'}可以等于大小写,取决于大小写,但暂时说$this->{$prefix . '_total'} = 4000 $this->{$prefix . '_total'} $this->{$prefix . '_total'} = 4000 $this->{$prefix . '_total'} = 4000

所以我用$this->{$prefix . '_total'} $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);
    }

尝试这个

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

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

echo (int)number_format(1000);

结果为“ 1”。 因为number_format产生“1000”,和(int)尝试来分析它放回的整数,并且因为“ ”不是一个整数规范的有效部分这就是它停止并且仅返回“ , 1 ”。 参见http://php.net/manual/en/language.types.string.php#language.types.string.conversion

这样的事情一定在您的Am_Currency::render发生了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM