The type of float for MySQL is very strange, how to solve
Source: Internet
Author: User
The type of float for MySQL is very strange
I have a table test with three fields in it.
Uid,one,two,uid is an int type, self-increment, one field is float (3,2) and the field is float (5,2).
According to the one field I can insert 123.12 such numbers, that is, the front three bits, the back two bits
Two fields I can insert the front 5 bit, the back two bit, but I insert a
INSERT into test values (null,123.12,12345.12) was inserted successfully, but the data displayed is not the same, which is why
If I need to insert the value 98.12, which is the front 2 bit, the decimal point two bit, this field should be set to float type
------Solution--------------------
1. You defined the wrong
MySQL allows non-standard syntax: FLOAT (M,D) or real (m,d) or double PRECISION (m,d). Here, "(M,d)" indicates that the value displays a total of M-bit integers, where the D-bit is behind the decimal point. For example, a column defined as float (7,4) can be displayed as-999.9999. MySQL is rounded when the value is saved, so if you insert 999.00009 in float (7,4) aligns, the approximate result is 999.0001.
2, with precision requirements should be used
The decimal and numeric types are considered to be the same type in MySQL. They are used to hold values that must be of exact precision, such as currency data. When declaring columns of that type, you can (and usually want to) specify the precision and scale, for example:
Salary DECIMAL (5,2)
In this example, 5 is precision, and 2 is the scale. The precision represents the primary number of digits that hold the value, and the scale represents the number of digits that can be saved after the decimal point.
------Solution--------------------
To limit positive numbers, you should set the unsigned property.
------Solution--------------------
It is recommended that you use the decimal field
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service