简体   繁体   中英

Rounding mysql 0.5 doesn't always go up

https://i.stack.imgur.com/pxEQW.png

CREATE TABLE `zz` (
  `jum_r` double DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `zz` VALUES (71045),(31875),(12045),(172125),(27325),(5465);


SELECT 
  jum_r, 
  ROUND(ROUND((jum_r * 1.1), 2), 2) as q_gross,
    ROUND(jum_r * 1.1) as gross,
    ROUND((jum_r * 10 / 100), 2) as q_ppn,
    ROUND(jum_r * 10 / 100) as ppn
FROM zz;

I have data according to the picture. Why does rounding 0.5 not always go up ...? What's wrong with my query? Thanks

For exact-precision numbers (eg DECIMAL) MySQL rounds 0.5 up to the next highest integer. For imprecise numbers (eg FLOAT) MySQL counts on the underlying C library's rounding, which is often "round-to-even". Doc ref here

After clarifying below, this should be your answer: CASE would help. Basically:

WHEN (ROUND(jum_r * 1.1) < 0.5) THEN FLOOR(ROUND(jum_r * 1.1)), WHEN (ROUND(jum_r * 1.1) >= 0.5 THEN CEILING(ROUND(jum_r * 1.1)). Not pretty but should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM