舍入数字:如果数字更接近 100,则向下舍入到最接近的 250,否则向上舍入到最接近的 250 [英] Rounding number: if number is closer to 100, round down to the nearest 250, else round up to the nearest 250
问题描述
我正在为一个整数做一个舍入代码.它已经在努力四舍五入到最近的 250
.
I'm doing a rounding code for a whole integer. It is already working to round to the nearest 250
.
Math.round(FormatNumber(CType(txt_input.Text, Decimal), 0) / 250) * 250
然而,这不是我需要的.相反,我需要相应地向上/向下舍入:
如果数字小于 100
,我需要向下舍入到最近的 250
否则向上舍入到最近的 250
.
However that's not what I need. Instead, I need to round up/down accordingly:
If number is less than 100
, I need to round down to the nearest 250
else round up to the nearest 250
.
示例:
17541 = 17500
2101 = 2250
7499 = 7500
7099 = 7000
我应该如何让应用程序相应地使用 Math.Floor()
或 Math.Ceiling()
?
How should I make the application use Math.Floor()
or Math.Ceiling()
accordingly?
推荐答案
您可以检查 100N Mod 250
是否为
.
如果是,则减去余数,否则加上 250
减去余数:
You could check whether N Mod 250
is < 100
.
If it is, subtract the remainder, otherwise add 250
minus the remainder:
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
这篇关于舍入数字:如果数字更接近 100,则向下舍入到最接近的 250,否则向上舍入到最接近的 250的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!