如何舍入到最接近的 0、5 或 9 整数

How to round to nearest 0, 5 or 9 integer

如何将给定数字四舍五入为 0、5 或 9,哪个最接近? 我试过:

=MROUND(I2,5)-((MOD(MROUND(I2,5),10))=0)

但我也需要零,这只会给我五和九。 谢谢

=1*(ROUNDDOWN(A2/10,0)&INDEX({0,5,9},MATCH(MIN(ABS({0,5,9}-MOD(A2,10))),ABS({0,5,9}-MOD(A2,10)),0)))
  • MOD 获取单元格的正确数字
  • 找出{0,5,9}与正确数字之间的距离,并将其与最接近的数字匹配
  • 将匹配数字的右侧连接到左侧数字并乘以 1

ROUNDDOWN 添加 0 以满足 ,生成以下内容:

根据我对你晦涩难懂的要求的了解,你需要取最后一个整数和任何分数(因此对于 123.456 我们将查看 3.456)并将其与此图表进行比较:

解读:

  If it is: Then it becomes:      
>= 0 And <2.5 0         
>= 2.5 And < 7 5         
>= 7 And < 9.5 9         
>= 9.5 and < 10 10 (zero rounded up)

代码:

Function round059(num As Double)
    Dim L As Single, R As Single
    L = Int(num / 10) * 10
    R = num - L
    If R >= 2.5 And R < 7 Then
        R = 5
    Else
        If R >= 7 And R < 9.5 Then
            R = 9
        Else
            If R >= 9.5 Then
                R = 10
            Else
                R = 0
            End If
        End If
    End If
    round059 = L + R
End Function

测试:

Sub test()
    Dim x As Double
    For x = -0.1 To 10.1 Step 0.2
        Debug.Print "    " & x & ":", , round059(x)
    Next x
End Sub

结果:

-0.1:                   0    
0.1:                    0    
0.3:                    0    
0.5:                    0    
0.7:                    0    
0.9:                    0    
1.1:                    0    
1.3:                    0    
1.5:                    0    
1.7:                    0    
1.9:                    0    
2.1:                    0    
2.3:                    0    
2.5:                    5    
2.7:                    5    
2.9:                    5    
3.1:                    5    
3.3:                    5    
3.5:                    5    
3.7:                    5    
3.9:                    5    
4.1:                    5    
4.3:                    5    
4.5:                    5    
4.7:                    5    
4.9:                    5    
5.1:                    5    
5.3:                    5    
5.5:                    5    
5.7:                    5    
5.9:                    5    
6.1:                    5    
6.3:                    5    
6.5:                    5    
6.7:                    5    
6.9:                    5    
7.1:                    9    
7.3:                    9    
7.5:                    9    
7.7:                    9    
7.9:                    9    
8.10000000000001:       9    
8.3:                    9    
8.5:                    9    
8.7:                    9    
8.9:                    9    
9.1:                    9    
9.3:                    9    
9.5:                    10    
9.7:                    10    
9.9:                    10    
10.1:                   10    

呀?