Rounding a number to the nearest 5 or 10 or X

前端 未结 13 1719
臣服心动
臣服心动 2020-11-28 08:11

Given numbers like 499, 73433, 2348 what VBA can I use to round to the nearest 5 or 10? or an arbitrary number?

By 5:

 499 ->  500
2348 -> 2350         


        
13条回答
  •  孤城傲影
    2020-11-28 08:20

    For a strict Visual Basic approach, you can convert the floating-point value to an integer to round to said integer. VB is one of the rare languages that rounds on type conversion (most others simply truncate.)

    Multiples of 5 or x can be done simply by dividing before and multiplying after the round.

    If you want to round and keep decimal places, Math.round(n, d) would work.

提交回复
热议问题