Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between float and decimal data type

Tags:

mysql

What difference does it make when I use float and decimal data types in MySQL?.

When should I use which?

like image 571
Hacker Avatar asked Mar 01 '11 03:03

Hacker


People also ask

What is the difference between float double and decimal?

A float has 7 decimal digits of precision and occupies 32 bits . A double is a 64-bit IEEE 754 double-precision floating-point number. 1 bit for the sign, 11 bits for the exponent, and 52 bits for the value. A double has 15 decimal digits of precision and occupies a total of 64 bits .

What is the difference between decimal and float in Python?

The main difference is Floats and Doubles are binary floating point types and a Decimal will store the value as a floating decimal point type. So Decimals have much higher precision and are usually used within monetary (financial) applications that require a high degree of accuracy.

Is a float number a decimal?

Integers and floats are two different kinds of numerical data. An integer (more commonly called an int) is a number without a decimal point. A float is a floating-point number, which means it is a number that has a decimal place. Floats are used when more precision is needed.


1 Answers

This is what I found when I had this doubt.

mysql> create table numbers (a decimal(10,2), b float); mysql> insert into numbers values (100, 100); mysql> select @a := (a/3), @b := (b/3), @a * 3, @b * 3 from numbers \G *************************** 1. row ***************************   @a := (a/3): 33.333333333   @b := (b/3): 33.333333333333 @a + @a + @a: 99.999999999000000000000000000000 @b + @b + @b: 100 

The decimal did exactly what's supposed to do on this cases, it truncated the rest, thus losing the 1/3 part.

So for sums the decimal is better, but for divisions the float is better, up to some point, of course. I mean, using DECIMAL will not give you a "fail proof arithmetic" in any means.

Hope this helps.

like image 143
kanap008 Avatar answered Oct 01 '22 01:10

kanap008