Jump to content

Simple arithmetic gone bad?


Samuz

Recommended Posts

So I added a number unformatted and got the expected value, but after I formatted it I received an incorrect value?

 

The Number unformatted looks like this: 441492111.66667. So did $number + 10 and got = 441492121.66667. That's all fine and correct.

 

But after I formatted like so:

 

$final = number_format($number) + 10;

 

I got 451.

 

Not too sure why that would happen, can someone explain to me please?

 

Link to comment
Share on other sites

The only thing that I find interesting here is that you are receiving 441 as your answer, where you should be receiving 441492111, do you want decimal places to be shown as well? Or do you want the number to be rounded as it is now?

See what you get when you have just this.

 

$final = number_format($number);

Link to comment
Share on other sites

It's quite correct answer from PHP :)

 

Read it first: http://ru.php.net/manual/en/function.number-format.php

 

What is written here?

If only one parameter is given, number will be formatted without decimals, but with a comma (",") between every group of thousands.

 

It means that 441492111.66667 is converted to a string "441,492,111.66667". When you try to add 10 to it, this string is converted to number 441!!! Then you add 10 to it and got 451 at the end. Quite correct :)

 

BTW what did you try to do?

 

PS. In the time when I type my answer another answer comes with approximately the same information... But I hope my answer will be interesting also.

Link to comment
Share on other sites

It's quite correct answer from PHP :)

 

Read it first: http://ru.php.net/manual/en/function.number-format.php

 

What is written here?

If only one parameter is given, number will be formatted without decimals, but with a comma (",") between every group of thousands.

 

It means that 441492111.66667 is converted to a string "441,492,111.66667". When you try to add 10 to it, this string is converted to number 441!!! Then you add 10 to it and got 451 at the end. Quite correct :)

 

BTW what did you try to do?

 

PS. In the time when I type my answer another answer comes with approximately the same information... But I hope my answer will be interesting also.

he would actually receive the string "441,492,111", but yes that makes sense.

Link to comment
Share on other sites

Thank you guys.

 

"It means that 441492111.66667 is converted to a string "441,492,111.66667". When you try to add 10 to it, this string is converted to number 441!!!"

 

That makes perfect sense.

 

So the ideal procedure would be to leave it as an integer while doing the math and then format it after i've generated the result?

Link to comment
Share on other sites

Thank you guys.

 

"It means that 441492111.66667 is converted to a string "441,492,111.66667". When you try to add 10 to it, this string is converted to number 441!!!"

 

That makes perfect sense.

 

So the ideal procedure would be to leave it as an integer while doing the math and then format it after i've generated the result?

correct, your first method of doing this was the correct way.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.