casting a floating to decimal in mysql - mysql

I am not quite understanding the cast function and decimal here. What does decimal(7,2) actually mean?. The first part and the second part arg of the function.
Why do i need to cast/convert my floating point to decimal here?.
mysql> SELECT productCode, MAX(price), MIN(price),
CAST(AVG(price) AS DECIMAL(7,2)) AS `Average`,
CAST(STD(price) AS DECIMAL(7,2)) AS `Std Dev`,
SUM(quantity)
FROM products
GROUP BY productCode;
+-------------+------------+------------+---------+---------+---------------+
| productCode | MAX(price) | MIN(price) | Average | Std Dev | SUM(quantity) |
+-------------+------------+------------+---------+---------+---------------+
| PEC | 0.49 | 0.48 | 0.49 | 0.01 | 18000 |
| PEN | 1.25 | 1.23 | 1.24 | 0.01 | 15000 |
+-------------+------------+------------+---------+---------+---------------+
Below is a sql fiddle for the same?.
http://sqlfiddle.com/#!2/1ed51b/1/0
My Questions again repeated:
What does decimal(7,2) actually mean?. The first part and the
second part arg of the function.
Why do i need to cast/convert my floating point to decimal here?.

DECIMAL(7,2) means a number with a fixed decimal point, having 7 digits altogether, with 2 of them to the right of the decimal point. (So, 5 left, 2 right.)
You don't need to cast unless you want the behavior of DECIMAL types (typically, fixed point) rather than the behavior of FLOAT types (useful approximations with a relatively unpredictable number of digits right of the decimal point). For example, your average for product code "PEC" with the cast is 33333.65; without the cast it's 33333.653333.
If you're routinely using all the digits, you should probably increase the total number of digits in the cast. Say, to something like DECIMAL (14,2).

Why? Because if the intention is just to display the values, then casting to a DECIMAL with two decimal places will make it easier to read. If you take out the CASTs you will be shown values like AVERAGE 33333.653333 STD DEV 47140.218734.

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.
If you use decimal(M,D) you are reserving no places for anything to the left of the decimal. Decimal(7,2) means 7-2 = 5 where 5 is your integer left and 2 being on right.
In simple words, it describes how many total digits a field will be able to store. Decimal(50,2) = 52 total digits, 50 before and 2 after decimal separator.

Related

How to store decimal in MySQL?

I've tried using DECIMAL with (2,2) but it won't let me use this.
I simply want to store a number, for example 7.50 or 10.50. I need to keep both numbers after the decimal though but when I refresh the database it resets the values to 0.99. Any suggestions?
The first parameter of the DECIMAL declaration is the total digits. You probably want to use DECIMAL (4, 2). This allows for up to two digits before the decimal and two after.
Documentation: https://dev.mysql.com/doc/refman/5.7/en/precision-math-decimal-characteristics.html
The syntax is DECIMAL(M,D)
M - total length
D - digits right of the decimal point
http://dev.mysql.com/doc/refman/5.6/en/fixed-point-types.html
The declaration syntax for a DECIMAL column is DECIMAL(M,D). The
ranges of values for the arguments in MySQL 5.6 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.
CREATE TABLE IF NOT EXISTS `table_name` (`id` int(11) NOT NULL AUTO_INCREMENT,`cost` DECIMAL( 10, 2 ) NOT NULL);
This will make the cost column hold a total of 10 digits, 8 before and 2 after the decimal point.
CREATE TABLE `salary` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`salary` DECIMAL(10,2) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
DECIMAL(10,2) indicates that salary will hold a total of 10 digits out of which 2 will be after the decimal.
i.e.
8 digits before decimal and 2 digits after decimal.
From mysql doc:
The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments 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.
NB:- M is total no. of digits before decimal point + total no. of digits after decimal point.
In your case, 7.50 has a total no of 3 digits and 10.50 has a total no of 4 digits. But the declared maximum no. of digits for the column is 2 so it can store a maximum of two digits value. You cannot even store 1 because it's 1.00 i.e. total 3 digits. So the maximum allowed 2 digit value in the column is .99.
If you want to store xx.xx then you have to declare (4, 2) where 4 is M and 2 is D.
If you want to store any number at maximum allowed size of mysql then you can declare a column with (65, 30).
Maximum no. of digits before decimal point = M - D
Change your field from INT to FLOAT

Price changes when using decimal data type

I have a column with the data type decimal setup as so..
`cost` decimal(4,2) NOT NULL DEFAULT '0.00',
However when I input a price, say 100.00, next time I look at it it says 99.99.
Why is this? I thought 4,2 allowed 4 chars before the decimal & 2 after?
4 is the precision (total number of digits), 2 is the scale (decimal places).
In your case you want 6,2.
A DECIMAL(M,D) column permits at most M - D digits to the left of the decimal point.
M is the maximum number of digits (the precision).
decimal [ (p[ ,s] )] and numeric[ (p[ ,s] )]
Fixed precision and scale numbers. When maximum precision is used, valid values are from - 10^38 +1 through 10^38 - 1. The ISO synonyms for decimal are dec and dec(p, s). numeric is functionally equivalent to decimal.
BOTTOM LINE:
you should try and make it 6,2.
6 as in total 6 digits, 2 as in 2 digits after the point.

What is the use of DECIMAL(x,0)?

In a DECIMAL(M, D) column MySQL gives the option for the range of D to be 0 to 30.
Is there a subtle reason that I'm missing for the option of 0? Isn't a decimal with nothing after the decimal point an integer?
When and why would I want to specify a DECIMAL that has no decimal places?
The number range of the DECIMAL type is much greater than for an INTEGER or BIGINT. The greatest number you are able to store in a DECIMAL(65, 0) is 65 nines. The largest number in a BIGINT is 18446744073709551615.
DECIMAL(x, 0) is often a little more expensive for small numbers. Consider using a defined INTEGER type if your numbers are in the range for one of those.
The storage requirement in bytes for a DECIMAL(x, 0) field depends on the x according to this formula:
Storage = x / 9 + Leftover
Leftover = round_up((x % 9) / 2) (i.e., about half of the leftover digits)
You can read more about storage requirements for numeric types in the MySQL manual and compare for yourself.
Besides allowing to store values bigger than BIGINT, you can use DECIMAL(x,0) if you want to:
allow values in the range -9, ... , +9: use DECIMAL(1,0) (uses 1 byte)
allow values in the range -99, ... , +99: use DECIMAL(2,0) (uses 1 byte)
allow values in the range -999, ... , +999: use DECIMAL(3,0) (uses 2 bytes)
allow values in the range -9999, ... , +9999: use DECIMAL(4,0) (uses 2 bytes)
...
allow values in the range -999999999, ... , +999999999: use DECIMAL(9,0) (uses 4 bytes)
... etc (up to DECIMAL(65,0) which uses 29 bytes)
In Mysql,
Decimal(3,2) means total 3 digits and 2 digits after decimal point like 3.42
Decimal(3,0) means in total 3 digits and no digit after decimal point like 345
Even if you write something beyond the given range in brackets of decimal Mysql will automatically update them to 000 or 999.
In a biging you can only store a digit which is no larger than 18 446 744 073 709 551 615. This is 20 digits, but in a DECIMAL you can specify even a 65 digits to store. Also with int you can't constrait directly the number of digits to a low number (e.g. to one). So it is more flexible, and if you need to expand it on an existing database, it is easier.

float precision problem in mysql

Can anybody, given example below, make to me an explanation how FLOAT works in mySQL? I know that float is approximative, but really, such a difference? And there is only 9 digits, so it is not overflow problem, isn't it?
mysql> create table t(f FLOAT(15,2), db DOUBLE);
mysql> insert into t(f,db) VALUES (512659663, 512659663);
mysql> select * from t;
+--------------+-----------+
| f | db |
+--------------+-----------+
| 512659648.00 | 512659663 |
+--------------+-----------+
(mysql Ver 14.14 Distrib 5.1.44, for Win32 (ia32) on Windows XP)
FLOAT is a 32-bit type with, as the name suggests, floating point. The higher is the value, the less is the absolute precision.
512659648 is large enough to introduce errors in tens.
Update:
In IEEE-754 (that's what FLOAT is), the data are stored in 32 bits: 1-bit sign, 8-bit binary exponent and 23-bit significand.
The exponent shows you the closest least power of 2 (28 in your case, or 268435456).
The significand is a binary fraction. It can store numbers from 1 to 2 with precision up to 2^-23. In your case, it's 1.11101000111010010000110, or ~ 1.9098060 in decimal notation.
The number is calculated as a product of the exponent and the significand.
Given all this, precision is 2 ^ (28 - 23) = 2 ^ 5 = 32 for the numbers of that order (2^28 to 2^29).
Actually, in MySQL, FLOAT and DOUBLE are both approximate floating-point numbers. MySQL uses four bytes for single-precision values and eight bytes for double-precision values.
So, both columns are approximate - it's just that your FLOAT column only gets 4 bytes to try to approximate your number.
The mantissa of a single precision floating point number is 22 bits in length. It therefore cannot accurately store an integer number greater than 2^22, 4194304.

What column type should I use to store values between 0 and 1 (say up to 5 decimal places) in MySQL?

It is most important that it be accurate, but also it should take the least disk space possible.
You would need a DECIMAL(6,5) to store a number from 0 to 1 with 5 decimal places.
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.
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.
According to this, in MySQL 5.0.3
DECIMAL(5,5) or DECIMAL (6,6) should take 3 bytes.
DECIMAL(4,4) 2 bytes.
If you need to store values from 0 to 1 inclusive, you might be tempted to use DECIMAL(6,5). But that occupies 4 bytes as integer and float parts are stored separately and you need one byte for integer and three for 5 decimal digits. And if you have 4 bytes you might as well use FLOAT.
Before MySql 5 DECIMALs were stored as strings and the most efficient way was to store SMALLINT or MEDIUMINT (2 or 3 bytes) and manually divide it by 10000 or 1000000 respectively.