【发布时间】:2014-04-18 05:13:19
【问题描述】:
我有这个变量:$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);
}
【问题讨论】:
-
你只是用
echo显示输出吗? -
不完全是,它在一个函数中。但如果我这样做 number_format("4000000000000000.00") 那么我得到 40
-
在包含
<?php echo number_format("4000000000000000.00") ?>的文件上运行 php 会为我打印 4,000,000,000,000,000。您确定字符串在输出之前没有被其他东西处理吗? -
请将完整的工作示例发布到3v4l.org 或类似网站,这并不是您描述的“可能”。
-
我猜是这样,但是功能很复杂在这里贴出来。问题是,如果我这样写:number_format($number) 我只会得到 4,而我应该得到 4000,但如果我这样写:number_format("4000000000000000.00") 那么我得到 40。奇怪,不是?
标签: php number-formatting