Is there an idiomatic way to round to the nearest multiple of a number?
number multiple result
12.2 0.5 12.0
12.2 0.25 12.25
12.4 0.5 12.5
Nearest multiple of 10 of any number (say x) is a number (say y) which is closest to the original number (i.e. x) and is divisible by 10 (i.e. y should be divisible by 10). So, for example the nearest multiple of 10 for the number 74 would be 70 whereas for 76 would be 80.
If you need to round a number to the nearest multiple of 5, you can use the MROUND function and supply 5 for number of digits. The value in B6 is 17 and the result is 15 since 15 is the nearest multiple of 5 to 17.
Python does provide a function that allows you to round a number, the round() function. The function is part of the normal Python library, meaning that you don't need to import anything.
Excel MROUND function Number - the value you want to round. Multiple - the multiple to which you want to round the number. For example, the formula =MROUND(7, 2) rounds 7 to the nearest multiple of 2 and returns 8 as the result.
The MROUND function rounds a number to the nearest given multiple. The multiple to use for rounding is provided as the significance argument. If the number is already an exact multiple, no rounding occurs and the original number is returned. You can use MROUND to round prices, times, instrument readings or any other numeric value.
The most common type of rounding is to round to an integer; or, more generally, to an integer multiple of some increment — such as rounding to whole tenths of seconds, hundredths of a dollar, to whole multiples of 1/2 or 1/8 inch, to whole dozens or thousands, etc.
Rounding to a specified multiple. When rounding to a predetermined number of significant digits, the increment m depends on the magnitude of the number to be rounded (or of the rounded result). The increment m is normally a finite fraction in whatever number system is used to represent the numbers.
As a general rule, rounding is idempotent; i.e., once a number has been rounded, rounding it again will not change its value. Rounding functions are also monotonic; i.e., rounding a larger number results in the same or larger result than rounding the smaller number. Typical rounding problems include:
You can define a function:
round_step(x, step) = round(x / step) * step
Usage:
julia> round_step(12.2, 0.25)
12.25
Such a function is actually used internally in Base for rounding numbers to a certain number of digits in a certain base:
julia> Base._round_step(12.2, 0.25, RoundNearest)
12.25
However since this is an internal implementation detail you shouldn't rely on this function. This function in turn calls _round_invstep
.
julia> Base._round_invstep(12.2, 4, RoundNearest)
12.25
julia> Base._round_invstep(12.4, 2, RoundNearest)
12.5
Which performs the operation round(x * invstep, r) / invstep
.
Because your examples happen to correspond to 0.1 in base 4 and base 2 you can also use round
directly for these particular cases:
julia> round(12.2; base=2, digits=1)
12.0
julia> round(12.2; base=4, digits=1)
12.25
julia> round(12.4; base=2, digits=1)
12.5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With