[英]Rounding mysql 0.5 doesn't always go up
https://i.stack.imgur.com/pxEQW.png 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 ...?为什么舍入 0.5 并不总是向上......? What's wrong with my query?我的查询有什么问题? Thanks谢谢
For exact-precision numbers (eg DECIMAL) MySQL rounds 0.5 up to the next highest integer.对于精确精度的数字(例如 DECIMAL),MySQL 将 0.5 向上舍入到下一个最高整数。 For imprecise numbers (eg FLOAT) MySQL counts on the underlying C library's rounding, which is often "round-to-even".对于不精确的数字(例如 FLOAT),MySQL 依靠底层 C 库的舍入,这通常是“舍入到偶数”。 Doc ref here 文档参考在这里
After clarifying below, this should be your answer: CASE would help.在下面澄清之后,这应该是您的答案:CASE 会有所帮助。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.