This site uses strictly necessary cookies. More Information

X# How do I round a number to the nearest .25 or .75 number?

Okay, so I know how to round a number to the nearest 0.5 by multiplying it by 2, rounding it, then dividing by 2 again.

I've figured out that doing the same my 4, instead of 2, gives me the nearest .25.

What I need, though, is to round it to the nearest .5, but at an offset of .25. So, instead of all numbers ending in .0 or .5, they should all end in .25 or .75

I can't figure out the maths for this. I know I can just add .25 to push it up, but that's not quite what I need.

**Answer** by EstragonHelmer
·
Apr 19, 2019 at 01:28 PM

Got it working!

For anyone else who needs this, here's the formula - it should also work with different offsets and scales.

```
output = ((Mathf.Round ((input - offset) * fraction)) / fraction) + offset;
```

input - float - mine was mouse positions

fraction - to change scale of rounding - mine was 2, to round to the nearest .5

offset - how much to slide your scale by - mine was 0.25, so I ended up with the nearest .25 or .75 number

output - the rounded number, to the correct offset

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.