How to store decimal in MySQL? - 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

Related

Given N bits, how many integers can be represented in binary?

Suppose you have 14 bits. How do you determine how many integers can be represented in binary from those 14 bits?
Is it simply just 2^n? So 2^14 = 16384?
Please note this part of the question: "how many INTEGERS can be represented in BINARY...". That's where my confusion lies in what otherwise seems like a fairly straightforward question. If the question was just asking how many different values or numbers can be represented from 14 bits, than yes, I'm certain it's just 2^n.
The answer depends on whether you need signed or unsigned integers.
If you need unsigned integers then using 2^n you can represent integers from 0 to 2^n exclusive. e.g. n=2; 2^2=4 you can represent the integers from 0 to 4 exclusive (0 to 3 inclusive). Therefore with n bits, you can represent a maximum unsigned integer value of 2^n - 1, but a total count of 2^n different integers including 0.
If you need signed integers, then half of the values are negative and half of the values are positive and 1 bit is used to indicate whether the integer is positive or negative. You then calculate using using 2^n/2. e.g. n=2; 2^2/2=2 you can represent the integers from -2 to 2 exclusive (-2 to +1 inclusive). 0 is considered postive, so you get 2 negative values (-2, -1) and 2 positive values (0 and +1). Therefore with n bits, you can represent signed integers values between (-) 2^n/2 and (+) 2^n/n - 1, but you still have a total count of 2^n different integers as you did with the unsigned integers.
Yes, it's that easy as 2^n.
A bit can have 2 distinct values: 0 and 1.
If you have 2 bits, than you have 4 distinct values: 00, 01, 10, 11. The list goes on.
Combinatorics has the simple counting formula
N = n_1 ⋅ n_2 ⋅ ... ⋅ n_k
Since n_1 = n_2 = n_k = 2 you can reduce the formula to
N = 2 ^ k

casting a floating to decimal in 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.

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.

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.