舍入数字:如果数字接近100,则向下舍入到最接近的250,否则舍入到最接近的250

时间:2019-01-29 20:42:15

标签: vb.net rounding

我正在为一个整数整数取整。它已经可以四舍五入到最接近的250

Math.round(FormatNumber(CType(txt_input.Text, Decimal), 0) / 250) * 250

但是那不是我所需要的。相反,我需要相应地向上/向下取整:
如果数字小于100,我需要四舍五入到最接近的250,否则四舍五入到最接近的250

示例:

17541 = 17500 
2101  = 2250
7499  = 7500
7099  = 7000 

如何使应用程序相应地使用Math.Floor()Math.Ceiling()

1 个答案:

答案 0 :(得分:4)

您可以检查N Mod 250是否为< 100
如果是,则减去余数,否则加250减去余数:

Dim n1 As Integer = 17541
Dim roundToValue As Integer = 250

Dim n1Remainder As Integer = n1 Mod roundToValue
Dim n2 As Integer = If(n1Remainder < 100, n1 - n1Remainder, n1 - n1Remainder + roundToValue)
'Or
'Dim n2 As Integer = (n1 - n1Remainder) + If(n1Remainder < 100, 0, roundToValue)

2101  -> 2250
7499  -> 7500
17541 -> 17500