Silly Rounding Question

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!

Im not sure what you mean?

The answers are correct, your expectations are apparently wrong…

Away from zero in negative numbers means lower.
Away from zero in positive number means higher.

What do YOU want to happen on 0.5? Tell us your goal and we’ll tell you how to do it.

edit Addl, it sounds to me like you want 0.5 to always round up. if this is the case, then you can accomplish that this way:

public float RoundItUp(float f){
    return Mathf.Floor(f+0.5f);
}

Mathf.Round isn’t broken; the way it works is intentional and described in the docs. It’s the same for System.Math.Round (which Mathf.Round is just a wrapper for anyway). The default rounding is MidPointRounding.ToEven because it’s statistically less biased. AwayFromZero is rounding in the correct direction and also behaves exactly as described in the docs…“away from zero” means exactly that; -4 is farther away from 0 than -3 is.

If you want rounding to behave in a non-standard way, you’d have to write your own function.

Sounds like you want Mathf.Ceil.

So, i found a solution that works, but is also a bit slow.
I read about rounding here: rounding -0.5

And what i got is this. You can imagine rounding to be like this: take x.y, add 0.5 to it and floor it. That should give you the proper results. So, 1 → 1.5 → 1. Or 0.5 → 1 - > 1. Or 1.2 → 1.7 → 1. Or -0.5 → 0 - > 0. Or -0.4 → 0.1 → 0. Or -0.7 → -0.2 → -1; Works great.

Now, we are dealing with floats here, so some precision problems arise. For example, 1/10 of the time i would get this issue where Mathf.Floor ( -3.0 ) would be -4, which means it was more like -3.00000000000000000001. So, here is what I did and it works:

Mathf.Floor ( 1000.0f * ( x + 0.5f ) ) / 1000.0f

It’s not ideal. I don’t like it, but at least it gives me answers I expect.

I used this rounding to determine grid position of any world object, whose position could be anything. Often than not, position were x.5 type. So, given a Vector3 position, this is how I would find my grid location:

int x = (int)( Mathf.Floor ( 1000.0f * (position.x / this.cellSize + 0.5f ) ) / 1000.0f ) + this.width / 2 - 1;
int z = (int)( Mathf.Floor ( 1000.0f * (position.z / this.cellSize + 0.5f ) ) / 1000.0f ) + this.depth / 2 - 1;

Where this.cellSize is the size of each grid cell. This.depth and this.width are the dimensions of the grid. Like I said, I don’t like this solution, so let me know if I can improve it somehow.

Thanks everyone!