I need to store data in a database and struggle to get my head around decimals or floats so need some help. The following are my expected ranges. I just need to know what data type I need to set for me columns in my Mysql database.
1) 7.2 - 9.2 (steps 0.2 so 7.4, 7.6,8.2 but also 8.0 etc)
2) 0 - 10.0 (steps in 0.25 so 0.25, 1.25, 2.75 but also whole number 5.0 etc)
You can use decimal , FLOAT OR DOUBLE
MySQL uses four bytes for single-precision values and eight bytes for double-precision values.
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.
When declaring a DECIMAL or NUMERIC column, the precision and scale
can be (and usually is) specified; for example:
salary DECIMAL(5,2) In this example, 5 is the precision and 2 is the
scale. The precision represents the number of significant digits that
are stored for values, and the scale represents the number of digits
that can be stored following the decimal point. If the scale is 0,
DECIMAL and NUMERIC values contain no decimal point or fractional
part.
Example
CREATE TABLE analyzes (COL1 DOUBLE(10,4), col2 decimal(7,5) ) ;
INSERT INTO analyzes VALUES(7.2,7.2 ),(0.2,0.2),(5.0,5.0);
http://sqlfiddle.com/#!9/f6efe6/1
Related
I am trying to pull data from Maria DB. The datatype in schema is DECIMAL (12, 8).
In my program when I query using following query. it trucates to 4 (or 3) decimal places and also round off.
select CAST(FORMAT(latitude, 100) AS FLOAT) latitude from mytable
it returns 36.173 . In DB it is stored as 36.17298200
I want it to return as 36.172982
To use the number in calculations, simply use it. No conversion, no CAST.
To dislay it to 6 decimal places, do that on SELECTing:
SELECT FORMAT(latitude, 6) ...
FORMAT(..., 100) should give you lots of decimal places.
FLOAT does not have enough precision to distinguish more than 6 or 7 significant decimal places. That is, the first and third numbers nere are the closest representable numbers in FLOAT:
x4210b122 --> 36.172981262207
36.17298200
x4210b123 --> 36.172985076904
Double:
x40421624463065f9 --> 36.1729819999999975
For Latitude and Longitude:
FLOAT has a resolution of 1.7 m or 5.6 ft -- good enough for Vehicles
DECIMAL(8,6) 16 cm 1/2 ft -- Friends in a mall
According to official MySQL documents:
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. In MySQL, NUMERIC is implemented as DECIMAL, so the following remarks about DECIMAL apply equally to NUMERIC.
Floating-Point Types (Approximate Value) - FLOAT, DOUBLE
So you should be writing something similar to:
select CAST(FORMAT(latitude, 100) AS DECIMAL(8,6)) latitude from mytable
As 8 is the total number of digits and 6 is precision.
I want do column for percents..
I need write: 1.00, 2.00, 99.99, 100.. %
How I can use double type for this?
Now I have:
Double(4,2)
But when I write 100 in column I get error:
Numeric value out of range: 1264 Out of range value for column
Look at the answer here (doubleand decimal have the same syntax) or the MySQL manual:
DOUBLE[(M,D)]
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.
In short, DOUBLE(4,2) means at most 4 digits, with 2 digits after the decimal point. So to support two digits decimals until 100, you need:
double(5,2) unsigned
Note that the unsigned is not mandatory, but can be more performant and safe if you know you won't need negative values.
double(4,2) means a double with a total of four digits, two of them right of the decimal point, meaning you only have 4-2=2 digits to the left of it. 100 has three digits to the left of the decimal point, hence the error. Using double(5,2) instead should fix the issue.
From MySQL docs
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
Coming to your problem, you need to increase size of M. i.e. Double(5,2)
I noticed one more thing will help someone.
IN DOUBLE(M,D) M is represent number of total digit including D, and D is represent the numbers after decimal,
If some one assign same value of M and D then you can only set value after decimal point but not before it.
Here i try to explain with an example,
If you want assign data type value like DOUBLE(5,5), so you can only enter a value like 0.00001 to 0.99999 but you can not 1.0001 or 1.0000.
There is also limit of assign D, you can not assign D greater then to M, its only M >= D
I need to store numbers like
21000
1.0002
0.00230235
12323235
0.2349523
This is sensordata so it is important to keep the exact value.
THere are many options.
My solution would be to multiply all values by 1 million, and store them as a bigint. Would that make sense?
That makes sense but I'd recommend that you just use the decimal datatype: https://dev.mysql.com/doc/refman/5.7/en/precision-math-decimal-characteristics.html
If you were to multiply by million and if a dataset you receive has one more decimal than you'd expect, you'd end up multiplying that number by 10 million and all other numbers by 10. Instead, using the decimal datatype will give you 30 numbers to the right of the decimal.
The declaration syntax for a DECIMAL column is DECIMAL(M,D). The
ranges of values for the arguments in MySQL 5.7 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.
and
The SQL standard requires that the precision of NUMERIC(M,D) be
exactly M digits. For DECIMAL(M,D), the standard requires a precision
of at least M digits but permits more. In MySQL, DECIMAL(M,D) and
NUMERIC(M,D) are the same, and both have a precision of exactly M
digits.
For a full explanation of the internal format of DECIMAL values, see
the file strings/decimal.c in a MySQL source distribution. The format
is explained (with an example) in the decimal2bin() function.
To format your numbers, you could do formatting like this answer describes: Format number to 2 decimal places
Example
create table test (
price decimal(40,20)
);
-- all the above insertions will succeed cleanly
insert into test values (1.5), (1.66), (1.777), (1.12345678901234567890);
-- notice we have 21 digits after decimal
-- MySQL will insert data with 20 decimal and add a warning regarding data truncation
insert into test values (1.123456789012345678901);
Data
select * from test
price
1.50000000000000000000
1.66000000000000000000
1.77700000000000000000
1.12345678901234567890
1.12345678901234567890
select cast(price as decimal(40,2)) from test
price
1.50
1.66
1.78
1.12
1.12
I have a MySQL table with some Decimal(4,2) fields.
I have a CSV file with numbers like 1485.23 to load in those fields.
After loading the file, the values in those fields is 99.99, like a max value to put inside.
Just alter your table in order to receive decimals more than 4 digits in total.
DECIMAL(4,2) means a number with 4 digits in total, 2 of them after floating point, so 99.99 will be maximum allowed.
DECIMAL(9,3) will receive numbers with 9 digits in total, 3 of them after floating point.
The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments in MySQL 5.7 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.
The maximum value of 65 for M means that calculations on DECIMAL values are accurate up to 65 digits. This limit of 65 digits of precision also applies to exact-value numeric literals, so the maximum range of such literals differs from before.
Example
In a DECIMAL column declaration, the precision and scale can be (and usually is) specified; for example:
salary DECIMAL(5,2)
In this example, 5 is the precision and 2 is the scale. The precision represents the number of significant digits that are stored for values, and the scale represents the number of digits that can be stored following the decimal point.
Standard SQL requires that DECIMAL(5,2) be able to store any value with five digits and two decimals, so values that can be stored in the salary column range from -999.99 to 999.99.
for more info https://dev.mysql.com/doc/refman/5.7/en/fixed-point-types.html
I need a field with percentages
25% => 0.25
30% => 0.30
Which data type do I need to set the field to?
And how can you determine the range of fields with decimals like integer fields? (eg. tinyint unsigned is 0-255)
Edit
I need to do some calculation in my queries
use DECIMAL DATATYPE
DECIMAL[(M[,D])] [UNSIGNED] [ZEROFILL]
For MySQL 5.0.3 and above:
A packed “exact” fixed-point number. M
is the total number of digits (the
precision) and D is the number of
digits after the decimal point (the
scale). The decimal point and (for
negative numbers) the “-” sign are not
counted in M. If D is 0, values have
no decimal point or fractional part.
The maximum number of digits (M) for
DECIMAL is 65 (64 from 5.0.3 to
5.0.5). The maximum number of supported decimals (D) is 30. If D is
omitted, the default is 0. If M is
omitted, the default is 10.
UNSIGNED, if specified, disallows
negative values.
All basic calculations (+, -, *, /)
with DECIMAL columns are done with a
precision of 65 digits.
note: if you want that that field maximum value between 0-99.99 then use
ALTER TABLE `table_name` ADD `percentage` DECIMAL( 5, 2 ) NOT NULL
Of the plethora of MySQL numeric types, the FLOAT or REAL may be your best bet (depends on what you're storing).
you should use FLOAT field with at leas 5 bits
How about decimal? :)
You can even set the precision:
CREATE TABLE yourtable ( percentage decimal(10,2) ... );
You definitely want a DECIMAL type; FLOAT suffers from the same imprecision that all floats do.