简体   繁体   中英

PHP Error in number_format

i have this variable: $this->{$prefix . '_total'} $this->{$prefix . '_total'} that can equal any number depending on the case but for the time being lets say $this->{$prefix . '_total'} = 4000 $this->{$prefix . '_total'} = 4000 .

So i use $this->{$prefix . '_total'} $this->{$prefix . '_total'} in order to get 4,000 .

However, i dont know why but i get 4 instead.

Whats happening to the other zeros?? I tried with different values such as 1564.13 for example and in all cases the output its only the very first number. So i tried it like this:

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

and it doesnt work either! I still get only the first digit. Why?? This blows my mind at so many levels. Please help.

Thank you.

Full function:

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

And this is the render function

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

Try this

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

Stabbing into the dark here to explain the phenomenon:

echo (int)number_format(1000);

This results in "1". Because number_format produces "1,000", and (int) tries to parse it back into an integer, and since " , " is not a valid part of an integer specification that's where it stops and returns only " 1 ". See http://php.net/manual/en/language.types.string.php#language.types.string.conversion .

Something like this must be happening in your Am_Currency::render .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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