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
Follow this Question
Related Questions
Need help putting integral function into C# 1 Answer
Applying a script to two objects not working 2 Answers
Error when importing mathematics 2 Answers
Multiple Lerp Calculations 0 Answers
Trying to clamp a second position to 45 degrees in 2d space 0 Answers