M.O.S. Studios Posted August 16, 2008 Share Posted August 16, 2008 hey guys a quick question i want a variable to have 2 decimal places even if the value is zero. so 18 = 18.00 is their an easy command for that or do I have to program code to check and add them? also if the value = 18.001 i want it to come out as 18.00 any tips help thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/119988-solved-variables-for-money/ Share on other sites More sharing options...
ignace Posted August 16, 2008 Share Posted August 16, 2008 number_format() you can find the correct syntax in the manual Quote Link to comment https://forums.phpfreaks.com/topic/119988-solved-variables-for-money/#findComment-618070 Share on other sites More sharing options...
M.O.S. Studios Posted August 16, 2008 Author Share Posted August 16, 2008 Awesome worked like a charm its now offical phpfreaks is the best website on the web. almost make me want to give up on mine seening as it could never be as awesome as this Quote Link to comment https://forums.phpfreaks.com/topic/119988-solved-variables-for-money/#findComment-618072 Share on other sites More sharing options...
cooldude832 Posted August 16, 2008 Share Posted August 16, 2008 number_format() you can find the correct syntax in the manual For simple actions this is fine the better way is to actually use the money_format function( http://us.php.net/money_format) and to treat the values as floats not integers. which means to use them as floats in your database with precision of 2 Quote Link to comment https://forums.phpfreaks.com/topic/119988-solved-variables-for-money/#findComment-618076 Share on other sites More sharing options...
Mchl Posted August 16, 2008 Share Posted August 16, 2008 For simple actions this is fine the better way is to actually use the money_format function( http://us.php.net/money_format) and to treat the values as floats not integers. which means to use them as floats in your database with precision of 2 There isn't such a thing as a 'float with a precision of 2'. What you mean is probably a numeric/decimal type. See SQL antipatterns presentation available in mysql forums here on phpfreaks to see the difference between the two. Quote Link to comment https://forums.phpfreaks.com/topic/119988-solved-variables-for-money/#findComment-618123 Share on other sites More sharing options...
cooldude832 Posted August 17, 2008 Share Posted August 17, 2008 This seems to say that you can specify the precision of a float http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html Quote Link to comment https://forums.phpfreaks.com/topic/119988-solved-variables-for-money/#findComment-618275 Share on other sites More sharing options...
Mchl Posted August 17, 2008 Share Posted August 17, 2008 Then it's not a float anymore, because float, as its name implies has floating point (and variable precision). But this is technical discussion about how types are implemented. Let's have an example, that shows how float and decimal types differ in practice. First a table CREATE TABLE `floatvsdecimal` ( `ID` int(10) unsigned NOT NULL auto_increment, `fl` float NOT NULL default '0', `decm` decimal(5,2) NOT NULL default '0.00', PRIMARY KEY (`ID`) ); Then data: insert 1000 rows like this one. INSERT INTO `floatsvsdecimal` VALUES (null,0.33,0.33); And now select: SELECT sum(`fl`), sum(`decm`) FROM floatvsdecimal; Results: +-----------------+-----------+ | sum(fl) | sum(decm) | +-----------------+-----------+ | 330.00001311302 | 330.00 | +-----------------+-----------+ 1000 rows, and there's a discrepancy on fifth decimal place. You need 1'000'000 rows for it to show up in second decimal place. If the values inserted into a table were more varied as in real life, it's possible that the discrepancy was even larger (or smaller). What's more, it can vary between hardware platforms and operating systems. Quote Link to comment https://forums.phpfreaks.com/topic/119988-solved-variables-for-money/#findComment-618430 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.