I searched around, but didnt find any straightforward answers. So, my problem is this:
I have values that come from positions and I am mapping them to a grid position.
Values could be anything, but are generally ending in .5, and this is where the problem arises. If I use regular Mathf.Round it gives completely asinine results, if I use Math.Round and specify MidpointRounding.AwayFromZero it doesn’t round in the right direction when it comes to negative numbers… like this:
-3.5 -> default: -4 awayFromZero: -4 (should be -3)
-2.5 -> default: -2 awayFromZero: -3 (should be -2)
-1.5 -> default: -2 awayFromZero: -2 (should be -1)
-0.5 -> default: 0 awayFromZero: -1 (should be 0)
0.5 -> default: 0 awayFromZero: 1 (should be 1)
1.5 -> default: 2 awayFromZero: 2 (should be 2)
2.5 -> default: 2 awayFromZero: 3 (should be 3)
3.5 -> default: 4 awayFromZero: 4 (should be 4)
So, I am no mathmatician, but default seems completely broken because it only gives even numbers. How does that make sense? Away From Zero rounds in the wrong direction when number is less than zero, because 0 > -1, -1 > -2 and 0.5 should round to a larger number (but i guess that’s what it is supposed to do). Is there a rounding function that actually does what I want?
EDIT:
I guess my questions is confusing. I have bunch of data points, which are floats, and I want to find closest ints. If value is x.000000…-x.49999999… i want it to round to x and if value is x.5 to x.9999999 i want to round to x+1. x.5 should always round x + 1. if x is -1, it should round 0. I have no problem with AwayFromZero, that makes sense. It does exactly what I would expect. But default behavior is odd to me, but I did read documentation and I understand what it is doing. Is there a way to round numbers in the manner I described? I imagine I would have to write my own code to do “normal” rounding, but I figured I would ask first.
EDIT2: Imagine a grid with 4 elements position in world space:
[-0.5, 0.5] [0.5, 0.5]
[-0.5, -0.5] [0.5, -0.5]
I would imagine that [-0.5, -0.5] would map to [0,0] in grid space and [0.5, 0.5] to [1,1]. That’s basically what I have. Objects dont always line up to 0.5, so i have to round.
Thanks!