MySQL cast as decimal not working as expected - mysql

This query:
SELECT CAST(30.123456789012345 AS DECIMAL(16,16))
Returns 0.9999999999999999.
Unless I got it completely wrong, it should return 30.1234567890123450.
Can anyone please explain what's going on?
Thanks!

You would need DECIMAL(18,16).
16,16 reserves all 16 available digits of precision for values to the right of the decimal point.

DECIMAL(18, 16) should work. As you can read on MySQL manual:
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 DECIMAL(16,16) tells MySQL to use all digits as decimal digits, without any integer part.

Related

MySQL ; Best data type for large numbers

I want to store a field which stores channel clicks per day( increasing every second and updated in a day) into MySQL DB table. What datatype should I assign to the column keeping in mind that it can even be less than 100 or can even exceed a million.
MySQL Integer Types (Exact Value):
Fixed-Point Types (Exact Value) - DECIMAL, NUMERIC:
In standard SQL, the syntax DECIMAL(M) is equivalent to DECIMAL(M,0).
Similarly, the syntax DECIMAL is equivalent to DECIMAL(M,0), where the
implementation is permitted to decide the value of M. MySQL supports
both of these variant forms of DECIMAL syntax. The default value of M
is 10.
If the scale is 0, DECIMAL values contain no decimal point or
fractional part.
The maximum number of digits for DECIMAL is 65, but the actual range
for a given DECIMAL column can be constrained by the precision or
scale for a given column. When such a column is assigned a value with
more digits following the decimal point than are permitted by the
specified scale, the value is converted to that scale. (The precise
behavior is operating system-specific, but generally the effect is
truncation to the permissible number of digits.)
Storage Requirements:

encountered many times this difficulty of decimal in MySQL

I encountered many times this problem of decimal in MySQL !
When i put this type: DECIMAL(10,8)
The maximum value allowed are: 99.99999999 !
It supposed to be: 9999999999.99999999 no ?
I want a maximum value of decimal with 8 digits after the point (.).
From the documentation:
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 first value is not the number of digits to the left of the decimal point, but the total number of digits.
That's why the value 9999999999.99999999 with DECIMAL(10, 8) is not possible: it is 18 digits long.
A decimal is defined by two parameters - DECIMAL(M, D), where M is the total number of digits, and D is number of digits after the decimal point out of M. To properly represent the number 9999999999.99999999, you'd need to use DECIMAL(18, 8).
The way DECIMAL(x,y) specifiers work is x represents the total number of digits and y the number that come after the decimal place.
10,8 means NN.NNNNNNNN.
If you want more, you need to make your range larger accordingly.
The first number is the total of digits, and the second one is the number of decimal places.
For the number you request, try DECIMAL(18,8).
You define DECIMAL(total positions, total decimal) so remember: the total decimal use the positions of the total positions, if you want most numbers to left and to right, use FLOAT type.

MYSQL: Cant save 1000000 on a float field

I have a float column and I'm trying to save the value 1000000. It automatically turns it to 1e+06. How can I fix it?
To have the value returned formatted as 1000000, you can simply add integer zero to the column in the SELECT list.
SELECT mycol+0 AS mycol FROM mytable
MySQL is storing the value IEEE floating point format. (One bit for sign, a certain number of bits for the exponent, and a certain number of bits for the mantissa. This isn't really a MySQL thing, it's the standard representation for floating point values.)
As far as what's being returned, that's an issue with converting that value into string representation.
A floating point number has a large range of values. To represent the maximum value of a float (3.402823e+38) as a decimal value, that would require 38 decimal digits. The seven left most digits of the value are significant, but we'd need to add another 32 zeros/digits to indicate the position of the decimal point.
So, returning a string representation of scientific notation is a reasonable approach to returning a representation of the value.
Those two things are equivalent:
1e+06
= 1 * 10^6
= 1 * 1,000,000
= 1,000,000
It's called scientific notation (see here). mySQL uses it to display huge/tiny values, especially approximate values (see here).
You can use DOUBLE(8, 3) where 8 is the total no. of digits excluding the decimal point, and 3 is the no. of digits to follow the decimal.

Why does the following produce an error?

I am trying to insert a value of 12,500,000.00 into an Access table and receive the following error message:
Decimal fields precision is too small to accept the numeric you attempted to add.
The field in the table is of data type Number and has the following properties:
Precision 19
Scale 14
Decimal Places 5
I don't understand because 12,500,000.00 has a precision of 8 and scale of 2. And decimal places is for display purposes only, not storage.
I fixed it by changing precision to 25, but would still appreciate some clarity.
Precision : The total number of digits that can be stored, both to the left and right of the decimal point
Scale : The maximum number of digits that can be stored to the right of the decimal separator
Decimal Places : The number of digits that are displayed the right of the decimal separator
In other words, by using a precision of 19, you were declaring your field to have 5 digits to the left of the decimal point, and 14 to the right (the value of Scale).
Changing the total precision to 25, allows 11 digits to be able to be stored to the left of the decimal point.
A decimal is a fixed-point number and when you set a scale of 14, there are actually 14 digits (in your case, zeroes) reserved right side of the decimal point. The scale is part of the precision.
It is different from what you expect in floating-point numbers when you can write 12,500,000.00 as 1.25e+7 and you have a precision of 3.

What does M,D mean in decimal(M,D) exactly?

Does anyone know about this?
As the docs say:
M is the maximum number of digits (the
precision). It has a range of 1 to 65.
(Older versions of MySQL allowed 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 M stands for Maximum (number of digits overall), D stands for Decimals (number of digits to the right of the decimal point).
https://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 allowed 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.
[Note: the link above has been updated to point to the MySQL 5.7 docs, but the text was quoted from the MySQL 5.1 docs.]
The doc says:
The declaration syntax for a DECIMAL column remains DECIMAL(M,D), although the range of values for the arguments has changed somewhat:
M is the maximum number of digits (the precision). It has a range of 1 to 65. This introduces a possible incompatibility for older applications, because previous versions of MySQL allow a range of 1 to 254. (The precision of 65 digits actually applies as of MySQL 5.0.6. From 5.0.3 to 5.0.5, the precision is 64 digits.)
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.