Doubles in MySQL - mysql

I created a table, which are contains some double, but when I added this number
7.341270020834490e+005
and the database is the following number:
734127.002083
cutting out some useful information. I want the hole number not just part of that
like:
734127.0020834490

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.
See the manual

You can store this as a string in your database....
and when you want to use the value just use some method in the language that you are using to convert string to double.

Related

Yii2 how to change precision for double type while migrating

I'm using MySQL database and trying to change column precision. I need to restrict numbers to 2 zeros afrer comma. But after execute this code: $this->alterColumn(offer::tableName(), 'price', $this->double(8.2)->null()); column precision in database is empty.
Result:
What I'm doing wrong?
If you want to restrict the numbers to 2 decimal numbers you should use fixed-point type like DECIMAL(8, 2).
In migration:
$this->decimal(8, 2)
Floating point types like FLOAT and DOUBLE allows only limited ways to set precision. There is support for FLOAT(M, D) and DOUBLE(M, D) syntax in MySQL. But, it's pretty much just an alias and you will always end with 4-byte float or 8-byte double. See documentation

Yii2: problems with findOne query on float value

Database field name with datatype value float(5,2)
Inserted value
7.80
78.00
My query in modal
$checkValue = static::find()->where(['value' => $this->value])->one();
If i passed $this->valueequal to 78.00 or 78.000 then it returns proper result.
But if I pass 7.80 or 7.8 then 0 rows are returned. Why?
i suspect internally mysql treats 7.8 as something like 7.800000000001 so you cannot get a result if you compare with a fixed value.
you may have come across mysql's reference manuals on datatypes.
please note the following:
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.
Because floating-point values are approximate and not stored as exact values, attempts to treat them as exact in comparisons may lead to problems. They are also subject to platform or implementation dependencies. For more information, see Section B.5.4.8, “Problems with Floating-Point Values”
For maximum portability, code requiring storage of approximate numeric data values should use FLOAT or DOUBLE PRECISION with no specification of precision or number of digits.
for most applications you can safely use a fixed point type.
essentially using decimal(5,2) instead of float(5,2) ensuring that any value displayed is the exact value stored internally.
when applicable rounding happens on insert with the "round half up" rule to the precision you specifed and is somewhat more intuitive and easy to manage
I resolve this issue with double datatype
value double(5,2)

How do I change precision of float values in mysql?

Hello in my database I have a column with numbers such as 6.251543423 I want to make them like 6.25 without rounding them.
I've tried update examresults set point = substring(point,0,5) but it returned all values as zero
If you don't want to round you must truncate:
TRUNCATE(6.251543423,2)
When you CAST as DECIMAL(n,2) or FLOAT(n,2) the result will be rounded.
Convert the floating point values to DECIMAL datatype, with appropriate number of digits after the decimal point. In this example, two digits after the decimal point.
SELECT CONVERT(float_col,DECIMAL(65,2)) AS dec_col
FROM ...
The floating point types FLOAT and DOUBLE are approximate decimal values. They are stored in standard IEEE floating point representation.
If you convert to decimal, and then store that back in a floating point column, it will be converted back into floating point representation.
You would need to do the conversion to DECIMAL when you pull the value back out again.
If you want to store an exact decimal representation, you would need to store the value in a column defined as DECIMAL (or NUMERIC) datatype, not a FLOAT or DOUBLE.
Also...
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,
(that's excerpted from MySQL Reference Manual: https://dev.mysql.com/doc/refman/5.5/en/floating-point-types.html )
(NOTE: I've never exercised/tested that behavior of DOUBLE(18,2). When we need exact decimals, we use DECIMAL. And when we need floating point, we use plain old DOUBLE.)

Not sure which MySQL data type to use

In general, I have double values that I work with. I use them as double values and also as strings (in application code). I store them as Double in my MySQL database.
The problem I have is with trailing 0's. For example, the value I get is 10.60. This value gets truncated down to 10.6 which is not ok for me. I need that trailing 0.
I also need it to not add 0's. For example, if I got 10.60, it should not add 0's to be 10.600. I am not sure which data type fits my needs. It needs to be stored as the double value, but keep its trailing 0's, and not add any additional 0's.
Can anyone assist me in which data type to use?
I would store the double values in a double/real field only and not varchar so as to not lose any precision during conversion. Since the issue is only in application code, I would round them to the appropriate decimal places (using the ROUND() function) while retrieving from the database.

Is it possible to cast a DECIMAL to DOUBLE in MySQL?

I know all the numerical implications, that is, the possible rounding issues inherent to floating point formats, but in my case I have DECIMAL columns in MySQL that I want to convert to DOUBLE straight in the MySQL query rather than down stream.
Could anyone help?
SELECT my_decimal_field + 0E0 FROM my_table
The following quotes from MySQL manual explain how this works:
9.1.2 Numeric Literals
Number literals include exact-value (integer and DECIMAL) literals and approximate-value (floating-point) literals.
Numbers represented in scientific notation with a mantissa and exponent are approximate-value numbers.
12.22.3 Expression Handling
Handling of a numeric expression depends on what kind of values the expression contains:
If any approximate values are present, the expression is approximate and is evaluated using floating-point arithmetic.
Because of the limitations of the built in CAST function in MySQL, it is only possible to convert DECIMAL to DOUBLE with your own user defined cast function.
Sample use case:
SELECT castDecimalAsDouble(0.000000000000000000100000000000);
Result: 1e-23
CREATE DEFINER=`root`#`localhost` FUNCTION `castDecimalAsDouble`(
decimalInput DECIMAL(65,30) ) RETURNS double
DETERMINISTIC
BEGIN
DECLARE doubleOutput DOUBLE;
SET doubleOutput = decimalInput;
RETURN doubleOutput;
END
It seems not possible to cast it to DOUBLE which brings problems if you do calculations and for example want to ROUND() a DECIMAL 12,2 in the third digit. Using ROUND(foo * bar,2) will just ignore the additional digits if your foo and bar are DECIMAL 12,2 fields.
That said you can do something like this to still make it work:
ROUND(CAST(foo AS DECIMAL(30,15)*CAST(bar AS DECIMAL(30,15)),2)
DECIMAL may save space. For example, DECIMAL(4,2) occupies only 2 bytes. FLOAT takes 4; DOUBLE takes 8.
As for the original question, simply do:
ALTER TABLE t MODIFY COLUMN c DOUBLE ...;
(The "..." should include the other stuff you already had, such as NOT NULL.)