mysql> CREATE TABLE ttt(id int, tt float(3,2));mysql> INSERT INTO ttt VALUES(1, '2.3424');mysql> INSERT INTO ttt VALUES(2, '200000.3434');mysql> SELECT * FROM ttt;
+------+-----------+
| id   | tt        |
+------+-----------+
|    1 |      2.34 |
|    2 | 200000.34 |
+------+-----------+
结果显示只保存两位小数,但是Float(3, 2)中的“3”的限制好像没起作用!

解决方案 »

  1.   

    The FLOAT type is used to represent approximate numeric datatypes. The SQL-92 standard allows an optional specification of the precision (but not the range of the exponent) in bits following the keyword FLOAT in parentheses. The MySQL implementation also supports this optional precision specification. When the keyword FLOAT is used for a column type without a precision specification, MySQL uses four bytes to store the values. A variant syntax is also supported, with two numbers given in parentheses following the FLOAT keyword. With this option, the first number continues to represent the storage requirements for the value in bytes, and the second number specifies the number of digits to be stored and displayed following the decimal point (as with DECIMAL and NUMERIC). When MySQL is asked to store a number for such a column with more decimal digits following the decimal point than specified for the column, the value is rounded to eliminate the extra digits when the value is stored. 
    这是mysql网站上对float的英文原文说明,那个3并不是mysql中文文档中的“位数”、“宽度”。而是存储的精度要达到的字节位数。具体如何存储,要多用数据来测试的。根据我的测试,float(3,2)能保证前7位的精度。
    如:
    mysql> INSERT INTO ttt VALUES(2, '123456789.3434');mysql> SELECT * FROM ttt;
    +------+---------------+
    | id   | tt            |
    +------+---------------+
    |    1 |          2.34 |
    |    2 |  123456792.00 |
    +------+---------------+
      

  2.   

    要实现楼主要求请使用NUMERIC 和 DECIMAL 类型