SQL货币汇率最接近0.05美分

car*_*mba 1 mysql math rounding

我想把MySQL SELECT中的钱花到最接近的0.05美分.

所以这样的数字:

140.70  should become 140.70
140.71  should become 140.70
140.72  should become 140.70
140.73  should become 140.75
140.74  should become 140.75
140.75  should become 140.75
140.76  should become 140.75
140.77  should become 140.75
140.78  should become 140.80
140.79  should become 140.80
Run Code Online (Sandbox Code Playgroud)

所以更详细

0.00 = 0.00
0.01 = 0.00
0.02 = 0.00
0.022 = 0.00   // here the magic should happen 0.022 is closer to 0, so result is 0
0.023 = 0.05   // but 0.023 should be rounded to 0.05! cause first round 0.023 to 0.025 which should then be rounded up to 0.05
0.03 = 0.05
Run Code Online (Sandbox Code Playgroud)

我用MySQL CEIL()MySQL FLOOR()尝试了一些不同的东西,但无法得到正确的结果.

在这里创建了一个SQL小提琴

有一个没有意义的表,除了我们需要一个SELECT来自:

CREATE TABLE hello ( world varchar(255) );
INSERT INTO hello (world) VALUES ('blubb');
Run Code Online (Sandbox Code Playgroud)

这是选择查询:

SELECT 

 CEILING ( 0.05 / 0.05 ) * 0.05 AS CEIL_1,
 CEILING ( 0.06 / 0.05 ) * 0.05 AS CEIL_2,
 CEILING ( 0.07 / 0.05 ) * 0.05 AS CEIL_3,
 CEILING ( 0.08 / 0.05 ) * 0.05 AS CEIL_4,
 CEILING ( 0.09 / 0.05 ) * 0.05 AS CEIL_5

FROM hello;
Run Code Online (Sandbox Code Playgroud)

这里有人告诉我该怎么做对吗?

Str*_*rry 7

SELECT ROUND(140.77/5,2) * 5;
+-----------------------+
| ROUND(140.77/5,2) * 5 |
+-----------------------+
|                140.75 |
+-----------------------+
Run Code Online (Sandbox Code Playgroud)