rcouser Posted February 2, 2011 Share Posted February 2, 2011 Hello there, I'm having a problem displaying money correctly with php. I have a field called "balance" with the type float(10,2), in this field I have a number store as "34.55" which I can go in and look at within phpmyadmin but when I echo this value on the front-end of the site it is displayed as "34.549999237061" Can someone please help? Regards. Quote Link to comment https://forums.phpfreaks.com/topic/226442-handling-money-with-php/ Share on other sites More sharing options...
BlueSkyIS Posted February 2, 2011 Share Posted February 2, 2011 do you have a question? Quote Link to comment https://forums.phpfreaks.com/topic/226442-handling-money-with-php/#findComment-1168848 Share on other sites More sharing options...
rcouser Posted February 2, 2011 Author Share Posted February 2, 2011 Yep, why is the 34.55 coming out of the database as 34.549999237061 and how do I make it display as 34.55 Quote Link to comment https://forums.phpfreaks.com/topic/226442-handling-money-with-php/#findComment-1168858 Share on other sites More sharing options...
BlueSkyIS Posted February 2, 2011 Share Posted February 2, 2011 the value in the database is probably really 34.549999237061, regardless of what you see in phpmyadmin. you can use money_format() or number_format() to display the value with only 2 decimal places. Quote Link to comment https://forums.phpfreaks.com/topic/226442-handling-money-with-php/#findComment-1168860 Share on other sites More sharing options...
PFMaBiSmAd Posted February 2, 2011 Share Posted February 2, 2011 You should be using a DECIMAL data type to store your values AND you are probably doing something in your php code that treats the number as a floating point value which results in a floating point conversion error. What's your actual code that is retrieving and displaying the value? Quote Link to comment https://forums.phpfreaks.com/topic/226442-handling-money-with-php/#findComment-1168864 Share on other sites More sharing options...
rcouser Posted February 2, 2011 Author Share Posted February 2, 2011 Thanks for the reply, I am working with a prebuilt system that uses float for currency. To retrieve the value is use the follow: <code> $conn = dbConnect('query'); $sql = 'SELECT id, balance FROM user WHERE id = ?'; $stmt = $conn->stmt_init(); if ($stmt->prepare($sql)) { $stmt->bind_param('i', $_GET['id']); $stmt->bind_result($id, $balance); $OK = $stmt->execute(); $stmt->fetch(); $stmt->free_result(); } echo $balance; </code> Quote Link to comment https://forums.phpfreaks.com/topic/226442-handling-money-with-php/#findComment-1168867 Share on other sites More sharing options...
jamesxg1 Posted February 2, 2011 Share Posted February 2, 2011 Is this what you are looking for? Change echo $balance; to this. setlocale(LC_MONETARY, 'en_GB'); echo money_format('%i', $balance); James. Quote Link to comment https://forums.phpfreaks.com/topic/226442-handling-money-with-php/#findComment-1168880 Share on other sites More sharing options...
rcouser Posted February 2, 2011 Author Share Posted February 2, 2011 Thanks james but that's not what i'm looking for. I added it to the database as 34.55 and I need it to come out as 34.55 so that if u update the value but adding 0.30, it can go back into the database as 34.85. The problem with using money_format() or number_format() is that it adds punctuation such as commas and full stops. Where does the 34.549999237061 come from? Quote Link to comment https://forums.phpfreaks.com/topic/226442-handling-money-with-php/#findComment-1168899 Share on other sites More sharing options...
jamesxg1 Posted February 2, 2011 Share Posted February 2, 2011 What are the properties of the `balance` ? Is is a varchar, int, text, ect? James. Quote Link to comment https://forums.phpfreaks.com/topic/226442-handling-money-with-php/#findComment-1168904 Share on other sites More sharing options...
rcouser Posted February 2, 2011 Author Share Posted February 2, 2011 The property of 'balance' is float(10,2) Quote Link to comment https://forums.phpfreaks.com/topic/226442-handling-money-with-php/#findComment-1168913 Share on other sites More sharing options...
jamesxg1 Posted February 2, 2011 Share Posted February 2, 2011 Ok in the query change balance to this, it may or may not work. FORMAT(`balance`, 2) James. Quote Link to comment https://forums.phpfreaks.com/topic/226442-handling-money-with-php/#findComment-1168929 Share on other sites More sharing options...
jamesxg1 Posted February 2, 2011 Share Posted February 2, 2011 May be very useful. http://stackoverflow.com/questions/2251290/storing-money-amounts-in-mysql James. Quote Link to comment https://forums.phpfreaks.com/topic/226442-handling-money-with-php/#findComment-1168930 Share on other sites More sharing options...
rcouser Posted February 2, 2011 Author Share Posted February 2, 2011 Thanks you very much for all the help James. Much Appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/226442-handling-money-with-php/#findComment-1168940 Share on other sites More sharing options...
jamesxg1 Posted February 2, 2011 Share Posted February 2, 2011 Thanks you very much for all the help James. Much Appreciated. No problem, my pleasure. James. Quote Link to comment https://forums.phpfreaks.com/topic/226442-handling-money-with-php/#findComment-1168959 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.