Number_Format() Causes Error "A Non Well Formed Numeric Value Encountered"

number_format() causes error A non well formed numeric value encountered

Try type casting first parameter of number_format() to float:

$format = number_format((float)0, 2);

or

$format = number_format(floatval(0), 2);

A non well formed numeric value encountered with number_format()

The number_format () function handles a numeric value, and returns that value formatted in string.

The error occurs with only more than four digits, because that is when you enter the thousands separator, "and the value "2" you entered means 2 digits of precision."

You can do as follows:

$value = (float)"1223.65";
echo ((float)number_format($value, 2) + 0);

See in PHP Documentation: http://php.net/manual/pt_BR/function.number-format.php

A non well formed numeric value encountered when calling number_format

PHP's number_format() accepts a numeric input. Make sure $price is numeric.

if ( ! function_exists('test_method'))
{
function test_method($var)
{
if (is_numeric($var)) {
return number_format($var);
}

// Invalid input, do something about it.
throw new \Exception("Invalid number to format: $var");
}
}

And your default value for $var is not a very good choice:

>>> number_format('')
PHP warning: number_format() expects parameter 1 to be float, string given on line 1

Update

The $price is already formatted. Obviously, the problem is that comma:

>>> number_format("524,800")
PHP error: A non well formed numeric value encountered on line 1

As you're saving prices with separators (seems to be already formatted), first you need to drop them before number formatting them:

if ( ! function_exists('test_method'))
{
function test_method($var)
{
// Prep
$var = str_replace(',', '', $var);

if (is_numeric($var)) {
return number_format($var);
}

// Invalid input, do something about it.
throw new \Exception("Invalid number to format: $var");
}
}

Think twice; it's not a good practice to save prices as formatted strings. Or if that's not your case, then you r data is being formatted somewhere else along the way before it hits your helper. Eaither way, you need to fix it.

A non well formed numeric value encountered After Formatted by number_format() function

may be this will help you. using str_replace remove comma from string.

$newOrderQuantity = str_replace(',', '', $order->order_quantity); // converting to "10000"

so on, you can use (int)$newOrderQuantity for calculation.

A non well formed numeric value encountered for number_format

I have updated the JavaScript to validate the amount using isNaN()

 var amount = $('#invoiceAmount').val();
if(isNaN(amount)){
alert ('Please enter in the valid amount.');
} else
// post values by AJAX
}

Notice: A non well formed numeric value encountered PHP

Try

return (float)number_format((float)str_replace(",", ".", (string) $b2Cost), 2, ".", "");

But if you just want to shorten the float, take a look at https://www.php.net/manual/en/function.round.php

How to solve a non well formed numeric value encountered in PHP?

number_format is function used for formatting strings. You may and should use it to view values, not to calculate them. You may change lines:

$grand_total = $tax + $sub_total;
$tax = number_format($tax,2);

But it would be a bit safer to use another variable:

$view_tax = number_format($tax,2);
$grand_total = $tax + $sub_total;
// now $grand_total uses $tax and to view
// tax, just use $view_tax variable

A non well formed numeric value encountered when converting integer to thousand separator

You can place an at symbol in front of the command to ignore all notices and such. It's bad form usually, but it should work.

echo @number_format($replace, 0 , '.' , ',' ) . '<br/>';

Edit: @ was in the wrong place.

Also, as was suggested, you could also check to be sure that the value is numeric:

http://php.net/manual/en/function.is-numeric.php

if(is_numeric($replace){
echo number_format($replace, 0 , '.' , ',' ) . '<br/>';
}

Edit Edit: You could also try typecasting the variable as mentioned here:

number_format() causes errors in Apache log

A non well formed numeric value encountered error

"non-well formed numeric value". You're passing a STRING in to gmdate(), which expects an integer timstamp.

You probably want

$time = $time_end - $time_start
$formatted = gmdate('H:i:s', (int)$time);
...
$stmt->bind_param('s', $formatted);

instead. Note the (int), since your timestart/end values are in floating point values, and date/gmdate expect time in seconds. PHP would convert for you, but I like making it explicit...

A non well formed numeric value encountered when calling number_format

PHP's number_format() accepts a numeric input. Make sure $price is numeric.

if ( ! function_exists('test_method'))
{
function test_method($var)
{
if (is_numeric($var)) {
return number_format($var);
}

// Invalid input, do something about it.
throw new \Exception("Invalid number to format: $var");
}
}

And your default value for $var is not a very good choice:

>>> number_format('')
PHP warning: number_format() expects parameter 1 to be float, string given on line 1

Update

The $price is already formatted. Obviously, the problem is that comma:

>>> number_format("524,800")
PHP error: A non well formed numeric value encountered on line 1

As you're saving prices with separators (seems to be already formatted), first you need to drop them before number formatting them:

if ( ! function_exists('test_method'))
{
function test_method($var)
{
// Prep
$var = str_replace(',', '', $var);

if (is_numeric($var)) {
return number_format($var);
}

// Invalid input, do something about it.
throw new \Exception("Invalid number to format: $var");
}
}

Think twice; it's not a good practice to save prices as formatted strings. Or if that's not your case, then you r data is being formatted somewhere else along the way before it hits your helper. Eaither way, you need to fix it.



Related Topics



Leave a reply



Submit