当前位置: 动力学知识库 > 问答 > 编程问答 >

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.

网友答案:

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.

网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: