i have a column in my table currently set as decimal(10,2)
but i want to be able to store 0.001299999999 which would round to 0.0013
how should this be stored in my MySQL table?
DECIMAL (10,8)
The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments in MySQL 5.7 are as follows:
M is the maximum number of digits (the precision). It has a range of 1 to 65.
D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.
Reference
Related
I need to store data in a database and struggle to get my head around decimals or floats so need some help. The following are my expected ranges. I just need to know what data type I need to set for me columns in my Mysql database.
1) 7.2 - 9.2 (steps 0.2 so 7.4, 7.6,8.2 but also 8.0 etc)
2) 0 - 10.0 (steps in 0.25 so 0.25, 1.25, 2.75 but also whole number 5.0 etc)
You can use decimal , FLOAT OR DOUBLE
MySQL uses four bytes for single-precision values and eight bytes for double-precision values.
FLOAT(M,D) or REAL(M,D) or DOUBLE PRECISION(M,D). Here, (M,D) means
than values can be stored with up to M digits in total, of which D
digits may be after the decimal point. For example, a column defined
as FLOAT(7,4) will look like -999.9999 when displayed. MySQL performs
rounding when storing values, so if you insert 999.00009 into a
FLOAT(7,4) column, the approximate result is 999.0001.
When declaring a DECIMAL or NUMERIC column, the precision and scale
can be (and usually is) specified; for example:
salary DECIMAL(5,2) In this example, 5 is the precision and 2 is the
scale. The precision represents the number of significant digits that
are stored for values, and the scale represents the number of digits
that can be stored following the decimal point. If the scale is 0,
DECIMAL and NUMERIC values contain no decimal point or fractional
part.
Example
CREATE TABLE analyzes (COL1 DOUBLE(10,4), col2 decimal(7,5) ) ;
INSERT INTO analyzes VALUES(7.2,7.2 ),(0.2,0.2),(5.0,5.0);
http://sqlfiddle.com/#!9/f6efe6/1
I need to store numbers like
21000
1.0002
0.00230235
12323235
0.2349523
This is sensordata so it is important to keep the exact value.
THere are many options.
My solution would be to multiply all values by 1 million, and store them as a bigint. Would that make sense?
That makes sense but I'd recommend that you just use the decimal datatype: https://dev.mysql.com/doc/refman/5.7/en/precision-math-decimal-characteristics.html
If you were to multiply by million and if a dataset you receive has one more decimal than you'd expect, you'd end up multiplying that number by 10 million and all other numbers by 10. Instead, using the decimal datatype will give you 30 numbers to the right of the decimal.
The declaration syntax for a DECIMAL column is DECIMAL(M,D). The
ranges of values for the arguments in MySQL 5.7 are as follows:
M is the maximum number of digits (the precision). It has a range of 1
to 65.
D is the number of digits to the right of the decimal point (the
scale). It has a range of 0 to 30 and must be no larger than M.
and
The SQL standard requires that the precision of NUMERIC(M,D) be
exactly M digits. For DECIMAL(M,D), the standard requires a precision
of at least M digits but permits more. In MySQL, DECIMAL(M,D) and
NUMERIC(M,D) are the same, and both have a precision of exactly M
digits.
For a full explanation of the internal format of DECIMAL values, see
the file strings/decimal.c in a MySQL source distribution. The format
is explained (with an example) in the decimal2bin() function.
To format your numbers, you could do formatting like this answer describes: Format number to 2 decimal places
Example
create table test (
price decimal(40,20)
);
-- all the above insertions will succeed cleanly
insert into test values (1.5), (1.66), (1.777), (1.12345678901234567890);
-- notice we have 21 digits after decimal
-- MySQL will insert data with 20 decimal and add a warning regarding data truncation
insert into test values (1.123456789012345678901);
Data
select * from test
price
1.50000000000000000000
1.66000000000000000000
1.77700000000000000000
1.12345678901234567890
1.12345678901234567890
select cast(price as decimal(40,2)) from test
price
1.50
1.66
1.78
1.12
1.12
What MySQL numeric data type can handle up to 22 digits of number?
You can use DECIMAL(M,D) and you can specify any
M is the maximum number of digits (the precision). It has a range of 1
to 65.
D is the number of digits to the right of the decimal point (the
scale). It has a range of 0 to 30 and must be no larger than M.
https://dev.mysql.com/doc/refman/5.7/en/precision-math-decimal-characteristics.html
You can try to use NUMERIC(22,0)
I have a MySQL table with some Decimal(4,2) fields.
I have a CSV file with numbers like 1485.23 to load in those fields.
After loading the file, the values in those fields is 99.99, like a max value to put inside.
Just alter your table in order to receive decimals more than 4 digits in total.
DECIMAL(4,2) means a number with 4 digits in total, 2 of them after floating point, so 99.99 will be maximum allowed.
DECIMAL(9,3) will receive numbers with 9 digits in total, 3 of them after floating point.
The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments in MySQL 5.7 are as follows:
M is the maximum number of digits (the precision). It has a range of 1 to 65.
D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.
The maximum value of 65 for M means that calculations on DECIMAL values are accurate up to 65 digits. This limit of 65 digits of precision also applies to exact-value numeric literals, so the maximum range of such literals differs from before.
Example
In a DECIMAL column declaration, the precision and scale can be (and usually is) specified; for example:
salary DECIMAL(5,2)
In this example, 5 is the precision and 2 is the scale. The precision represents the number of significant digits that are stored for values, and the scale represents the number of digits that can be stored following the decimal point.
Standard SQL requires that DECIMAL(5,2) be able to store any value with five digits and two decimals, so values that can be stored in the salary column range from -999.99 to 999.99.
for more info https://dev.mysql.com/doc/refman/5.7/en/fixed-point-types.html
How to properly divide two DECIMAL values in MySQL and get a result accurate to the number of digits defined in the column type?
Example:
select cast(1/2 as decimal(4,4)),
cast(1 as decimal(4,4))/2,
cast(1 as decimal(4,4))/cast(2 as decimal(4,4));
Results:
'0.5000', '0.49995000', '1.00000000'
Why is the second result innacurate? Why is the third result not '0.5000'?
Note: I can't just use the first form, I need to perform calculations with columns stored as decimals.
The problem is that DECIMAL data type declaration requires 2 arguments, the first one being the total number of digits, including the fractional part.
According to the docs (http://dev.mysql.com/doc/refman/5.7/en/precision-math-decimal-characteristics.html):
The declaration syntax for a DECIMAL column is DECIMAL(M,D). The
ranges of values for the arguments in MySQL 5.1 are as follows:
M is the maximum number of digits (the precision). It has a range of 1
to 65. (Older versions of MySQL permitted a range of 1 to 254.)
D is the number of digits to the right of the decimal point (the
scale). It has a range of 0 to 30 and must be no larger than M.
So if you use 5 total digits instead of 4, everything is ok:
select cast(1/2 as decimal(5,4)),
cast(1 as decimal(5,4))/2,
cast(1 as decimal(5,4))/cast(2 as decimal(5,4)),
cast(cast(1 as decimal(5,4))/cast(2 as decimal(5,4)) as decimal(5,4));
Results
0.5000, 0.50000000, 0.50000000, 0.5000