MySQL, changing the default rounding to more than four digits

Just found out that this query:

SELECT 1 / 1000000;

will return 0, which is a small disaster for our economic program when performing Vata calculations. How to set decimal by default without changing every request?

Edit

Let us give an example: to receive a part of cotton from the net cost, with 6% VAT. 6 / 106 = 0.056603773...but SELECT 6 / 106 = 0.0566.

+4
source share
3 answers

As described in Arithmetic Operators :

, /, - div_precision_increment ( 4). , 5.05 / 0.014 (360.714286).

, :

  • div_precision_increment :

    SET div_precision_increment = 6;
    

    , (.. ):

    SET GLOBAL div_precision_increment = 6;
    

    [mysqld] / , , .

  • , :

    SELECT CAST(1 / 1000000 AS DECIMAL(10,8));
    
+2

. sql 10.

select cast(1 / 1000000 as decimal(10,10));

EDIT: . :

SELECT 6.000000/106.000000;

, . , .

+1

:

SELECT 10000 * (6/106)

, : 566.0377

, , cents, :

SELECT ROUND(10000*(6/106),2)

, () DECIMAL. , ​​

  DECIMAL(10,2) 

// .

. ,

 SELECT 24*(6/106)*CAST(1.20 AS DECIMAL(10,2))

will give 6% of the net VAT for two dozen pieces, the cost of which is 1.20.

To do this without unnecessary queries, you may need to ALTER some definitions of your columns.

I tried this on MariaDB 10, MySQL 5.1, 5.5 and 5.6. Seems good.

+1
source

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


All Articles