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

时间:2018-03-16 22:43:29

标签: excel google-sheets formulas

如何将给定数字舍入到最接近的0,5或9? 我试过了:

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

但我也需要零,这只能给我五分和九分。 感谢

2 个答案:

答案 0 :(得分:4)

=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

添加0到ROUNDDOWN以满足,产生以下内容:
here
plot

答案 1 :(得分:3)

根据我对您的晦涩请求的理解,您需要获取最后一位数和任何分数(因此123.456我们将查看3.456)并将其与此图表进行比较:< / p>

解释

<强>   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    

雅?