Calculate MODULO?

Hi Guys,

I want to calculate a value in steps of 5.
17 -> 20
12 -> 15
24.5 -> 25
is this somehow possible using Modulo ?

lG
Daisy

Use ceil.

const x = 17
Math.ceil(x / 5) * 5; /* 20 */
1 Like

This should also work, using basic operators only.

x - x % 5 + 5

To floor instead of ceil, just omit the + 5.

That doesn't seem to achieve the desired result

Never mind, I read what you actually typed now!

You missed the first part of the equation.

12 - 12 % 5 + 5 = 15

1 Like

Am I missing something as it does not work for 5, 10, 15, etc.

15- 15 % 5 + 5 = 20

You're right... :woozy_face:

One shouldn't do math before coffee! :see_no_evil: Embarrassing!

I think we should stick with Marcus' solution! :rofl:

2 Likes

It’s rare I get these things!
Maths isn’t my strong point, like at all!

My thinks whoever comes here looking to "Calculate MODULO" is not gonna be happy :wink:

Perhaps the title should be updated to "How do I round up a number?"

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.