jakebur01 Posted September 26, 2019 Share Posted September 26, 2019 I'm attempting to multiply a number. It does fine unformatted, but when I add number_format and the number is above 1,000 with a comma separator, it has problems. I tried formatting with out the comma, then adding back in after the calculation, but that did not work. $value = 1000; $multiplier = 1.02; for ($i = 1; $i <= 10; $i++) { $value = number_format($value, 2, '.', ''); $value = $value * $multiplier; $value = number_format($value, 2); echo "$$value<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/309284-trouble-with-number_format/ Share on other sites More sharing options...
jakebur01 Posted September 26, 2019 Author Share Posted September 26, 2019 Man, 10 years ago this used to be a hot site! Where did everybody go? Quote Link to comment https://forums.phpfreaks.com/topic/309284-trouble-with-number_format/#findComment-1569994 Share on other sites More sharing options...
Barand Posted September 26, 2019 Share Posted September 26, 2019 The number value of the string "1,000" is 1. (ie anything befor the first non-numeric character.) Do all your maths on unformatted numbers and only format imediately prior to output. $value = 1000; $multiplier = 1.02; for ($i = 1; $i <= 10; $i++) { $value *= $multiplier; echo number_format($value, 2) . '<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/309284-trouble-with-number_format/#findComment-1569995 Share on other sites More sharing options...
ginerjm Posted September 26, 2019 Share Posted September 26, 2019 When you 'format' a numeric value it no longer is a number - it's a STRING! Quote Link to comment https://forums.phpfreaks.com/topic/309284-trouble-with-number_format/#findComment-1569998 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.