tags:

views:

37

answers:

2

When using decimal in MySQL, does a value of (5,3) make it so the max number is 99.999? if not how do I specify that? My number is for currency so I need to use decimal, but it should not go over 99 and it must have 3 decimal places.

+1  A: 

The method you specified should work. I don't see any problems with it.

Patrick
Thanks for your assurance @Patrick, but I was looking for a little more explanation.
typoknig
+2  A: 

decimal (5,3) is the standard SQL way to define a field with a range of -99.999 .. 99.999.

Note that with MySQL, if you attempt to insert a value that is out of range, MySQL will automatically truncate the value to the nearest boundary value (in this case, either 99.999 or -99.999) without throwing an error.

Added

In this case, there will be a maximum of 5 digits of data stored, with exactly 3 digits to the right of the decimal point. If you insert a number like 1.23 it will be stored as 1.230; likewise 1 will be stored as 1.000. If you insert 12.3456 it will be rounded up to 12.346; if you insert 12.0001 it will be rounded down to 12.000.

When the column is returned to your program, the exact representation will depend upon how your database API maps a decimal data type. If it maps to an integer, you'll lose the decimal fraction; if it maps to a floating point number (float, double), you may lose precision; if it maps to a Decimal (C#/Java) then you'll get an exact representation, as long as your value is within the bounds of the datatype.

Craig Trader
Thanks, but to be clear on one more thing, does there HAVE to be three decimal places after the decimal or could it be just one or two? Likewise, does the number HAVE to be 5 digits, or is 5 just the max?
typoknig