Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL DECIMAL data type

Tags:

I have a column in MySQL with the type DECIMAL(2,1). When I insert the number 10 into the database it reads 9.9. I want it to read 10.0. Any ideas? Thanks.

like image 766
dbj44 Avatar asked Sep 30 '12 20:09

dbj44


People also ask

What is decimal datatype in MySQL?

The MySQL DECIMAL data type is used to store exact numeric values in the database. We often use the DECIMAL data type for columns that preserve exact precision e.g., money data in accounting systems. To define a column whose data type is DECIMAL you use the following syntax: column_name DECIMAL(P,D);

How do I get 2 decimal places in MySQL?

SELECT ROUND(-4.535,2); Explanation: The above MySQL statement will round the given number -4.535 up to 2 decimal places.

What is the data type for a decimal number?

Decimal is not a floating-point data type. The Decimal structure holds a binary integer value, together with a sign bit and an integer scaling factor that specifies what portion of the value is a decimal fraction.

How does decimal in MySQL work?

In MySQL, the DECIMAL and NUMERIC data types store exact, fixed-point values. In MySQL, NUMERIC is implemented as a DECIMAL, so a NUMERIC and DECIMAL are the same data type. This data type is used when it is important to preserve the exact precision, such as storing money data.


1 Answers

DECIMAL(2,1) means (as the manual suggests) a decimal of 2 characters wide (in total!) and 1 decimal. If you want 10.0, you need DECIMAL(3,1) (three wide, one decimal).

like image 185
Bart Friederichs Avatar answered Sep 28 '22 19:09

Bart Friederichs