Samuz Posted December 21, 2011 Share Posted December 21, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/253608-simple-arithmetic-gone-bad/ Share on other sites More sharing options...
AyKay47 Posted December 21, 2011 Share Posted December 21, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/253608-simple-arithmetic-gone-bad/#findComment-1300079 Share on other sites More sharing options...
SergeiSS Posted December 21, 2011 Share Posted December 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253608-simple-arithmetic-gone-bad/#findComment-1300081 Share on other sites More sharing options...
AyKay47 Posted December 21, 2011 Share Posted December 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253608-simple-arithmetic-gone-bad/#findComment-1300082 Share on other sites More sharing options...
Samuz Posted December 21, 2011 Author Share Posted December 21, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/253608-simple-arithmetic-gone-bad/#findComment-1300098 Share on other sites More sharing options...
AyKay47 Posted December 21, 2011 Share Posted December 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253608-simple-arithmetic-gone-bad/#findComment-1300118 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.