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
Related
I want do column for percents..
I need write: 1.00, 2.00, 99.99, 100.. %
How I can use double type for this?
Now I have:
Double(4,2)
But when I write 100 in column I get error:
Numeric value out of range: 1264 Out of range value for column
Look at the answer here (doubleand decimal have the same syntax) or the MySQL manual:
DOUBLE[(M,D)]
M is the total number of digits and D is the number of digits following the decimal point. If M and D are omitted, values are stored to the limits permitted by the hardware. A double-precision floating-point number is accurate to approximately 15 decimal places.
In short, DOUBLE(4,2) means at most 4 digits, with 2 digits after the decimal point. So to support two digits decimals until 100, you need:
double(5,2) unsigned
Note that the unsigned is not mandatory, but can be more performant and safe if you know you won't need negative values.
double(4,2) means a double with a total of four digits, two of them right of the decimal point, meaning you only have 4-2=2 digits to the left of it. 100 has three digits to the left of the decimal point, hence the error. Using double(5,2) instead should fix the issue.
From MySQL docs
MySQL permits a nonstandard syntax:
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
Coming to your problem, you need to increase size of M. i.e. Double(5,2)
I noticed one more thing will help someone.
IN DOUBLE(M,D) M is represent number of total digit including D, and D is represent the numbers after decimal,
If some one assign same value of M and D then you can only set value after decimal point but not before it.
Here i try to explain with an example,
If you want assign data type value like DOUBLE(5,5), so you can only enter a value like 0.00001 to 0.99999 but you can not 1.0001 or 1.0000.
There is also limit of assign D, you can not assign D greater then to M, its only M >= D
I'm trying to truncate a double with 4 decimals, but I get the last digit rounded (and the double has 4 decimals only!)
double:
-2.5805
SELECT TRUNCATE(double, 4) FROM `table`
Result:
-2.5804
What's more interesting is that:
SELECT TRUNCATE(mycol, 4) AS col1, TRUNCATE(-2.5805, 4) AS col2
FROM mytable
returns:
col1 | col2
--------+--------
-2.5804 | -2.5805
Demo here
The double data type is a floating point type, meaning these numbers are not stored as exact numbers. As MySQL documentation on floating point types explains:
Because floating-point values are approximate and not stored as exact
values, attempts to treat them as exact in comparisons may lead to
problems. They are also subject to platform or implementation
dependencies.
This means that the number -2.5805 is not an exact number when it is stored as a double. The truncate() function, however, returns an exact number with an exact number of digits. When the -2.5805 (as double) is expanded, the 4th decimal digit is 4.
The TRUNCATE(-2.5805, 4) expression returns -2.5805 because MySQL treats numeric literals expressed with fixed number of decimal digits as decimal data type, which is an exact data type. Therefore, the truncate() function just simply returns the same number of decimal digits.
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
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
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