Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between float(2,2) and float() in mysql

I created a table in MySQL with a column of type float(2,2) and inserted value 10 into the same, but I found that the value stored is 0.99.

What is the reason behind this ? If we use a column of type float(m, n) in MySQL where 'm' and 'n' are integers, then what should I watch out for when storing values in this column ?

like image 399
ARUN P.S Avatar asked Nov 02 '11 11:11

ARUN P.S


People also ask

What does float 2 mean?

float(2,2) means "use two digits, of which two are used after the decimal point". Hence, you can go from -0.99 to 0.99. You can't insert 10 into this column, you'd need atleast two digits before the comma (so float(4,2) or float(2,0)).

What is the difference between float and double float?

float and double both have varying capacities when it comes to the number of decimal digits they can hold. float can hold up to 7 decimal digits accurately while double can hold up to 15.

What is the difference between float and double in MySQL?

A FLOAT is for single-precision, while a DOUBLE is for double-precision numbers. MySQL uses four bytes for single-precision values and eight bytes for double-precision values. There is a big difference from floating point numbers and decimal (numeric) numbers, which you can use with the DECIMAL data type.

What is the difference between a double and a float 2 differences?

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 .


2 Answers

float(2,2) means "use two digits, of which two are used after the decimal point".

Hence, you can go from -0.99 to 0.99. You can't insert 10 into this column, you'd need atleast two digits before the comma (so float(4,2) or float(2,0)).

MySQL permits a nonstandard syntax: FLOAT(M,D) or REAL(M,D) or DOUBLE PRECISION(M,D). Here, “(M,D)” means than values can be stored with up to M digits in total, of which D digits may be after the decimal point. For example, a column defined as FLOAT(7,4) will look like -999.9999 when displayed. MySQL performs rounding when storing values, so if you insert 999.00009 into a FLOAT(7,4) column, the approximate result is 999.0001.

like image 85
Konerak Avatar answered Sep 24 '22 10:09

Konerak


MySQL permits a nonstandard syntax: FLOAT(M,D) or REAL(M,D) or DOUBLE PRECISION(M,D). Here, “(M,D)” means than values can be stored with up to M digits in total, of which D digits may be after the decimal point.

like image 26
Francis Avatar answered Sep 24 '22 10:09

Francis