Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to round to nearest multiple?

Tags:

julia

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
like image 460
Alec Avatar asked Oct 30 '21 18:10

Alec


People also ask

What do you mean by nearest multiple?

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.

How do you round to the nearest multiple of 5?

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.

How do you round a number to the nearest multiple in Python?

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.

How do I round up to the nearest multiple in Excel?

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.

How do you round a number to the nearest multiple?

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.

What is the most common type of rounding?

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.

What does it mean to round to a specified number?

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.

What is the rule of rounding in math?

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:


1 Answers

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
like image 164
ahnlabb Avatar answered Nov 13 '22 22:11

ahnlabb