数字格式中的PHP错误
我有一个变量:数字格式中的PHP错误,php,number-formatting,Php,Number Formatting,我有一个变量:$this->{$prefix.\u-total'},根据具体情况,它可以等于任何数字,但暂时让我们假设$this->{$prefix.\u-total'}=4000 所以我使用$this->{$prefix.\u total'}来获得4000 然而,我不知道为什么,我得到了4 其他零发生了什么事??我尝试使用不同的值,例如1564.13,在所有情况下,输出都只是第一个数字。所以我试着这样做: number_format(($this->{$prefix . '_total'
$this->{$prefix.\u-total'}
,根据具体情况,它可以等于任何数字,但暂时让我们假设$this->{$prefix.\u-total'}=4000
所以我使用$this->{$prefix.\u total'}
来获得4000
然而,我不知道为什么,我得到了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;
}
试试这个
<?php
echo number_format($number,0,'',',');
?>
在这里向黑暗中刺来解释这种现象:
echo (int)number_format(1000);
这将导致“1”。因为number\u format
生成“1000”,而(int)
尝试将其解析回整数,并且由于“,
”不是整数规范的有效部分,因此它停止并仅返回“1
”。看
在您的
Am\u Currency::render
中一定会发生类似的情况,您是否只是使用echo
显示输出?不完全是,它位于函数内部。但是如果我像这个数字格式(“4000000000000000.00”)那样做,那么我会在一个包含
的文件上运行40个php,为我打印4000000000000000。您确定字符串在输出之前没有被其他人处理吗?请将完整的工作示例发布到或类似的文件中,这并不像您所描述的那样“可能”。我想是的,但是在这里发布它的函数非常复杂。问题是,如果我这样写:number\u格式($number),我只能得到4,而我应该得到4000,但如果我这样写:number\u格式(“4000000000000000.00”),那么我得到40。奇怪,不是吗?它太长了,不能在这里作为包含多个文件的完整脚本的一部分发布。嗨,我在代码中添加了这个函数。看来这是所有这些的罪魁祸首。
echo (int)number_format(1000);