Archive for the ‘decimal’ Category

MySQL Decimal datatype

Апрель 21st, 2010
The DECIMAL datatype is a datatype in MySQL that provides additional precision over FLOATs or DOUBLEs at the expense of arithmetic. You specify the precision by using
datatype-name DECIMAL
or
datatype-name DECIMAL(8)
or
datatype-name DECIMAL(10,4)

MySQL uses roughly 4 bytes for every 9 digits either side of the decimal.

The english definition (http://www.thefreedictionary.com/decimal) would assume that when you specify the DECIMAL datatype you would want to insert numbers with fractions.

This however is not the case. Lets show this:

mysql> create database decimal_madness;
Query OK, 1 row affected (0.00 sec)

mysql> use decimal_madness;
Database changed
mysql> create table test1 ( tmp decimal);
Query OK, 0 rows affected (0.10 sec)

mysql> insert into test1 values(1.1);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+------------------------------------------+
| Level | Code | Message |
+-------+------+------------------------------------------+
| Note | 1265 | Data truncated for column 'tmp' at row 1 |
+-------+------+------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from test1;
+------+
| tmp |
+------+
| 1 |
+------+
1 row in set (0.00 sec)

mysql> create table test2 ( tmp decimal(4));
Query OK, 0 rows affected (0.05 sec)

mysql> insert into test2 values(1.1);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+------------------------------------------+
| Level | Code | Message |
+-------+------+------------------------------------------+
| Note | 1265 | Data truncated for column 'tmp' at row 1 |
+-------+------+------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from test2;
+------+
| tmp |
+------+
| 1 |
+------+
1 row in set (0.00 sec)

mysql> create table test3 ( tmp decimal(4,2));
Query OK, 0 rows affected (0.35 sec)

mysql> insert into test3 values(1.1);
Query OK, 1 row affected (0.01 sec)

mysql> select * from test3;
+------+
| tmp |
+------+
| 1.10 |
+------+
1 row in set (0.00 sec)

PlanetMySQL Voting: Vote UP / Vote DOWN

MySQL silliness — Decimal datatype

Апрель 12th, 2010
The DECIMAL datatype is a datatype in MySQL that provides additional precision over FLOATs or DOUBLEs at the expense of arithmetic. You specify the precision by using
datatype-name DECIMAL
or
datatype-name DECIMAL(8)
or
datatype-name DECIMAL(10,4)

MySQL uses roughly 4 bytes for every 9 digits either side of the decimal.

Logically you would assume that when you specify the DECIMAL datatype you would want to insert numbers with fractions.

The default however is not to include any fraction part. Lets show this:

mysql> create database decimal_madness;
Query OK, 1 row affected (0.00 sec)

mysql> use decimal_madness;
Database changed
mysql> create table test1 ( tmp decimal);
Query OK, 0 rows affected (0.10 sec)

mysql> insert into test1 values(1.1);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+------------------------------------------+
| Level | Code | Message |
+-------+------+------------------------------------------+
| Note | 1265 | Data truncated for column 'tmp' at row 1 |
+-------+------+------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from test1;
+------+
| tmp |
+------+
| 1 |
+------+
1 row in set (0.00 sec)

mysql> create table test2 ( tmp decimal(4));
Query OK, 0 rows affected (0.05 sec)

mysql> insert into test2 values(1.1);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+------------------------------------------+
| Level | Code | Message |
+-------+------+------------------------------------------+
| Note | 1265 | Data truncated for column 'tmp' at row 1 |
+-------+------+------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from test2;
+------+
| tmp |
+------+
| 1 |
+------+
1 row in set (0.00 sec)

mysql> create table test3 ( tmp decimal(4,2));
Query OK, 0 rows affected (0.35 sec)

mysql> insert into test3 values(1.1);
Query OK, 1 row affected (0.01 sec)

mysql> select * from test3;
+------+
| tmp |
+------+
| 1.10 |
+------+
1 row in set (0.00 sec)

PlanetMySQL Voting: Vote UP / Vote DOWN

MySQL silliness — Decimal datatype

Апрель 12th, 2010
The DECIMAL datatype is a datatype in MySQL that provides additional precision over FLOATs or DOUBLEs at the expense of arithmetic. You specify the precision by using
datatype-name DECIMAL
or
datatype-name DECIMAL(8)
or
datatype-name DECIMAL(10,4)

MySQL uses roughly 4 bytes for every 9 digits either side of the decimal.

Logically you would assume that when you specify the DECIMAL datatype you would want to insert numbers with fractions.

The default however is not to include any fraction part. Lets show this:

mysql> create database decimal_madness;
Query OK, 1 row affected (0.00 sec)

mysql> use decimal_madness;
Database changed
mysql> create table test1 ( tmp decimal);
Query OK, 0 rows affected (0.10 sec)

mysql> insert into test1 values(1.1);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+------------------------------------------+
| Level | Code | Message |
+-------+------+------------------------------------------+
| Note | 1265 | Data truncated for column 'tmp' at row 1 |
+-------+------+------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from test1;
+------+
| tmp |
+------+
| 1 |
+------+
1 row in set (0.00 sec)

mysql> create table test2 ( tmp decimal(4));
Query OK, 0 rows affected (0.05 sec)

mysql> insert into test2 values(1.1);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+------------------------------------------+
| Level | Code | Message |
+-------+------+------------------------------------------+
| Note | 1265 | Data truncated for column 'tmp' at row 1 |
+-------+------+------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from test2;
+------+
| tmp |
+------+
| 1 |
+------+
1 row in set (0.00 sec)

mysql> create table test3 ( tmp decimal(4,2));
Query OK, 0 rows affected (0.35 sec)

mysql> insert into test3 values(1.1);
Query OK, 1 row affected (0.01 sec)

mysql> select * from test3;
+------+
| tmp |
+------+
| 1.10 |
+------+
1 row in set (0.00 sec)

PlanetMySQL Voting: Vote UP / Vote DOWN

MySQL silliness — Decimal datatype

Апрель 12th, 2010
The DECIMAL datatype is a datatype in MySQL that provides additional precision over FLOATs or DOUBLEs at the expense of arithmetic. You specify the precision by using
datatype-name DECIMAL
or
datatype-name DECIMAL(8)
or
datatype-name DECIMAL(10,4)

MySQL uses roughly 4 bytes for every 9 digits either side of the decimal.

Logically you would assume that when you specify the DECIMAL datatype you would want to insert numbers with fractions.

The default however is not to include any fraction part. Lets show this:

mysql> create database decimal_madness;
Query OK, 1 row affected (0.00 sec)

mysql> use decimal_madness;
Database changed
mysql> create table test1 ( tmp decimal);
Query OK, 0 rows affected (0.10 sec)

mysql> insert into test1 values(1.1);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+------------------------------------------+
| Level | Code | Message |
+-------+------+------------------------------------------+
| Note | 1265 | Data truncated for column 'tmp' at row 1 |
+-------+------+------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from test1;
+------+
| tmp |
+------+
| 1 |
+------+
1 row in set (0.00 sec)

mysql> create table test2 ( tmp decimal(4));
Query OK, 0 rows affected (0.05 sec)

mysql> insert into test2 values(1.1);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+------------------------------------------+
| Level | Code | Message |
+-------+------+------------------------------------------+
| Note | 1265 | Data truncated for column 'tmp' at row 1 |
+-------+------+------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from test2;
+------+
| tmp |
+------+
| 1 |
+------+
1 row in set (0.00 sec)

mysql> create table test3 ( tmp decimal(4,2));
Query OK, 0 rows affected (0.35 sec)

mysql> insert into test3 values(1.1);
Query OK, 1 row affected (0.01 sec)

mysql> select * from test3;
+------+
| tmp |
+------+
| 1.10 |
+------+
1 row in set (0.00 sec)

PlanetMySQL Voting: Vote UP / Vote DOWN

MySQL silliness — Decimal datatype

Апрель 12th, 2010
The DECIMAL datatype is a datatype in MySQL that provides additional precision over FLOATs or DOUBLEs at the expense of arithmetic. You specify the precision by using
datatype-name DECIMAL
or
datatype-name DECIMAL(8)
or
datatype-name DECIMAL(10,4)

MySQL uses roughly 4 bytes for every 9 digits either side of the decimal.

Logically you would assume that when you specify the DECIMAL datatype you would want to insert numbers with fractions.

The default however is not to include any fraction part. Lets show this:

mysql> create database decimal_madness;
Query OK, 1 row affected (0.00 sec)

mysql> use decimal_madness;
Database changed
mysql> create table test1 ( tmp decimal);
Query OK, 0 rows affected (0.10 sec)

mysql> insert into test1 values(1.1);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+------------------------------------------+
| Level | Code | Message |
+-------+------+------------------------------------------+
| Note | 1265 | Data truncated for column 'tmp' at row 1 |
+-------+------+------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from test1;
+------+
| tmp |
+------+
| 1 |
+------+
1 row in set (0.00 sec)

mysql> create table test2 ( tmp decimal(4));
Query OK, 0 rows affected (0.05 sec)

mysql> insert into test2 values(1.1);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+------------------------------------------+
| Level | Code | Message |
+-------+------+------------------------------------------+
| Note | 1265 | Data truncated for column 'tmp' at row 1 |
+-------+------+------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from test2;
+------+
| tmp |
+------+
| 1 |
+------+
1 row in set (0.00 sec)

mysql> create table test3 ( tmp decimal(4,2));
Query OK, 0 rows affected (0.35 sec)

mysql> insert into test3 values(1.1);
Query OK, 1 row affected (0.01 sec)

mysql> select * from test3;
+------+
| tmp |
+------+
| 1.10 |
+------+
1 row in set (0.00 sec)

PlanetMySQL Voting: Vote UP / Vote DOWN