Home > other >  PHP Format number expects parameter 2 to be int, what am I doing wrong?
PHP Format number expects parameter 2 to be int, what am I doing wrong?

Time:07-25

I'm trying to calculate a discount with php based on the total order and how much the product costs at the time of purchase. The underlying string should work fine according to the php manual https://www.php.net/manual/en/function.number-format.php but I get the error Warning: number_format () expects parameter 2 to be int, string given online 142.

My line 142 is this: <span >'. (($regular_price - $total))|number_format (1, '.', ','). ' € ('. (($regular_price - $total) / $regular_price*100)|number_format (1, '.' , ','). ' %)</span>

Maybe I'm missing something? Can anyone clarify this? I appreciate any response, thanks.

CodePudding user response:

You need to specify decimals.

number_format(
    float $num,
    int $decimals = 0,
    ?string $decimal_separator = ".",
    ?string $thousands_separator = ","
): string

Try to use it like this:

<span >'. number_format ((($regular_price - $total)), 1, '.', ','). ' € ('. number_format ((($regular_price - $total) / $regular_price*100), 1, '.' , ','). ' %)</span>

CodePudding user response:

You're getting the syntax wrong, try this way Reference: https://www.w3schools.com/php/func_string_number_format.asp

<span >'. number_format( ($regular_price - $total),2 ) .'€ '. number_format( (($regular_price - $total) / $regular_price*100),1 ) .'%</span>
  •  Tags:  
  • php
  • Related