I've searched this up a little bit but I haven't found the correct information for my request. In my MySQL table I am storing a double, some examples of the double would be 5.60 or 2.44. I'm wondering if I could store a double with a max length just like INT(9). Would I use Decimal(2,2) or would I use Double(2)?
For FLOAT and DOUBLE, in MySQL the precision value is used only to determine storage size. A precision from 0 to 23 results in a 4-byte single-precision FLOAT column. A precision from 24 to 53 results in an 8-byte double-precision DOUBLE column.
The DECIMAL and NUMERIC types store exact numeric data values. These types are used when it is important to preserve exact precision, for example with monetary data.
Related
I have column of type float(12,2) in my table. Why did the number 1399999.05 was rounded to 1399999.00 during the insert operations? As far as I know it rounds numbers if their count of digits more then set in float description. But my number had definitely just two digits.
MySQL version - 5.7
You need the DOUBLE data type here. FLOAT only allows about seven significant decimal digits of precision. The numbers represented by the text strings 1399999.05 and 1399999.00 are indistiguishable from each other when converted to IEEE 32-bit FLOAT values.
MySQL ignores the precision specification for FLOAT, DOUBLE, INT (and TINYINT, SMALLINT, BIGINT) data types. Instead it uses the native data type. MySQL honors the precision specification for DECIMAL data.
In your case your precision spec is (12,2).
Please consider reading this: What Every Computer Scientist Should Know About Floating Point Numbers.
And, for a hint about what can go wrong when you lose precision you need, read this.
As laravel migration I put a field as double() without assign the number of digits of decimals and totals. In the mysql db are stored as double without, again, numbers of digits.
So I try to search in laravel and mysql docs but with no hope, the question is What is the default rappresentation of a double field without the numbers of digits?
Ok the are used 8 bytes, but how?
thanks
Do not use FLOAT(m,n) or DOUBLE(m,n); use only FLOAT or DOUBLE. The (m,n) causes double-rounding and/or truncation. The intent of FLOAT and DOUBLE is represent "scientific" (not "money") values that have a wide range of precision and range.
FLOAT holds about 7 significant digits; DOUBLE, 16. The decimal point can be virtually anywhere.
If you want "fixed point" numbers, use DECIMAL(m,n). For example DECIMAL(8,2) can represent up to a million dollars or Euros, down to the cent.
I am defining my column's length as double(16,2). Here 16 is not fixed. But 2 is fixed. Display length will vary depending on calculation. So I want to make it variable length.
How do I define the number of digits to the right of the decimal point (i.e. 2 from my example) but have display length variable? Because I don't want to occupy unnecessary space.
The FLOAT and DOUBLE types represent approximate numeric data values.
MySQL uses four bytes for single-precision values and eight bytes for double-precision values.
MySQL also supports this optional precision specification, but the precision value is used only to determine storage size. A precision from 0 to 23 results in a 4-byte single-precision FLOAT column. A precision from 24 to 53 results in an 8-byte double-precision DOUBLE column.
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.
FLOAT:
A small (single-precision) floating-point number. Permissible values are -3.402823466E+38 to -1.175494351E-38, 0, and 1.175494351E-38 to 3.402823466E+38. These are the theoretical limits, based on the IEEE standard. The actual range might be slightly smaller depending on your hardware or operating system.
A single-precision floating-point number is accurate to approximately 7 decimal places.
This is not required and will default to 10,2,
(If M and D are omitted, values are stored to the limits permitted by the hardware.)
DOUBLE:
M is the total number of digits and D is the number of digits following the decimal point. If M and D are omitted, values are stored to the limits permitted by the hardware. A double-precision floating-point number is accurate to approximately 15 decimal places.
This is not required and will default to 16,4, where 4 is the number of decimals.
( If M and D are omitted, values are stored to the limits permitted by the hardware. )
For more
http://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html
The MySQL DOUBLE data type occupies 8 bytes of storage no matter what numbers you store in it.
In MySQL, when you define a double or a float you define the precision like double(10,0). I also noticed you can define those types without a precision (which I assume means the maximum possible value). What is the maximum values for these datatypes and how would it be defined in sql (the (10,0) part of the datatype)?
Cheers
From a simple google search "mysql data types" you can get to the mySQL manual page:
http://dev.mysql.com/doc/refman/5.0/en/floating-point-types.html
This describes the maximum precision of a 4 byte single precision float as 23 and the maximum of a double precision 8 byte float as 53.
Will there be any data loss when converting between double and decimal in mysql?
Doubles have ~16 decimal digits of precision. So the answer is no, since both types are declared to have 15 decimal digits of precision. (If the types had 16 digits, it would depend on the number stored since the precision of doubles is actually between 15 and 16 digits.)
The maximum number of digits for DECIMAL is 65 (64 from MySQL 5.0.3 to 5.0.5). Before MySQL 5.0.3, the maximum range of DECIMAL values is the same as for DOUBLE, but the actual range for a given DECIMAL column can be constrained by the precision or scale for a given column. Manual
I would guess no.
A DOUBLE-precision floating-point number is accurate to approximately 15 decimal places. DECIMAL has a precision of 65 digits. You wouldn't lose any data when converting from DOUBLE to DECIMAL. As Brad says, previously the maximum range of DECIMAL values were the same as for DOUBLE. Numeric Type Overview
If you are doing numeric computations on your data and need more that 15 decimal places, I would suggest you take the time and convert your columns to DECIMAL since DOUBLE stores an approximation of the value and not the actual value.