Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/119988-solved-variables-for-money/
Share on other sites

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

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.