MySQL TRUNCATE (double, 4) changes the value instead of truncating

I try to truncate a double with 4 decimal places, but I get the last digit rounded (and the double has only 4 decimal places!)

twice: -2.5805

SELECT TRUNCATE(double, 4) FROM `table`

Result: -2.5804

What's even more interesting:

SELECT TRUNCATE(mycol, 4) AS col1, TRUNCATE(-2.5805, 4) AS col2
FROM mytable

returns:

col1    |  col2
--------+--------
-2.5804 | -2.5805

Demo here

+4
source share
1 answer

The double data type is a floating-point type, that is, these numbers are not stored as exact numbers. Since the MySQL documentation for floating point types explains:

, . .

, -2.5805 , double. truncate() . -2.5805 ( double), 4.

TRUNCATE(-2.5805, 4) -2.5805, MySQL , , . truncate() .

+3

Source: https://habr.com/ru/post/1655326/


All Articles