ubiquibacon
ubiquibacon

Reputation: 10667

MySQL Decimal Clarification

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.

Upvotes: 3

Views: 2112

Answers (2)

Craig Trader
Craig Trader

Reputation: 15679

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.

Upvotes: 5

Patrick
Patrick

Reputation: 170

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

Upvotes: 1

Related Questions