2

I get the following error when I try to use number format in php:

Notice: A non well formed numeric value encountered

$number = 10345.34543;
$format = "0, ',', '.'";
echo number_format($number, $format);

I am almost positive it is because I'm holding the formating part in a string, but is there any way around this?

Thanks.

1
  • 1
    Read. The answers are there. You're completely misusing the function. Commented Dec 12, 2010 at 16:56

1 Answer 1

1

The format you are trying to pass the arguments in is not possible. You are passing a string instead of an int.

The expected parameters are:

string number_format ( float $number , 
                       int $decimals = 0 , 
                       string $dec_point = '.' , 
                       string $thousands_sep = ',' )

you will need to pass the arguments accordingly:

echo number_format($number, 0, ",", ".");

If you have a special reason to do this the way you show, you would have to use eval() which is highly discouraged, or maybe call_user_func_array() but you would have to explain in detail first what you are trying to do.

Sign up to request clarification or add additional context in comments.

2 Comments

My reason for doing this is that I'd like to store the format in the DB. $format is actually a string that I'm getting from my DB. What would be the best way to go about this?
@David I would either store the values separately, or use something like sprintf() which allows storing a #.## format pattern without the security implications

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.