fastsol Posted October 25, 2019 Share Posted October 25, 2019 So I am trying to take a string value of dollars (posted from a form) and times it by 100 to get the cents Integer value of the dollars. For some reason if I do this with something like 278.53 I end up with 27852. Same concept for 278.59 ends up as 27858. I don't understand why or how to make it work. I've tried many things and nothing has made it work. $price = '278.53'; // posted from the form $cents = $price * 100; // converting to cents. end_result = (int)$cents // This will end up being 27852 not 27853. Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 25, 2019 Share Posted October 25, 2019 Money is not an int. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted October 25, 2019 Share Posted October 25, 2019 this is due to floating point conversion errors. see this link on how to do this math using binary coded decimal numbers, i.e. like a hand-held calculator does - https://www.php.net/manual/en/book.bc.php Quote Link to comment Share on other sites More sharing options...
Barand Posted October 25, 2019 Share Posted October 25, 2019 $price = 278.53; $cents = $price * 100; $end_result = $cents ; // without the (int) gives 27853 Quote Link to comment Share on other sites More sharing options...
requinix Posted October 26, 2019 Share Posted October 26, 2019 (int) will truncate the number. The actual value is something like 27852.99999999997. Spoiler for most floating-point problems: round(). 1 Quote Link to comment Share on other sites More sharing options...
fastsol Posted October 26, 2019 Author Share Posted October 26, 2019 10 hours ago, requinix said: (int) will truncate the number. The actual value is something like 27852.99999999997. Spoiler for most floating-point problems: round(). round was the one thing I didn't try, Thank you! This gives the correct result. $price = '278.53'; $cents = $price * 100; $end_result = (int)round($cents); // Gives me 27853 as expected. also works for 278.59 I knew the issue was a precision thing, I just couldn't find the right combination of functions to make it work. In the end it was so simple (as I figured it would be). Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 26, 2019 Share Posted October 26, 2019 Couldn't you just use the str_replace to remove the dot? Quote Link to comment Share on other sites More sharing options...
requinix Posted October 26, 2019 Share Posted October 26, 2019 6 hours ago, ginerjm said: Couldn't you just use the str_replace to remove the dot? Please no. It is a number. Treat it like a number. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 27, 2019 Share Posted October 27, 2019 Actually the OP began this topic by stating that he was working on a 'string' value (from a form). If he is willing to move forward with that and is comfortable that the users will input a valid currency amount, my suggestion made sense. Afterall, it doesn't appear to be a mathematical situation here, simply a formatting change. Quote Link to comment 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.