- Home /

# 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!

Can you reproduce that with a test case?

With this code

```
Debug.Log (Math.Round(-3.5f, MidpointRounding.AwayFromZero));
Debug.Log (Math.Round(-2.5f, MidpointRounding.AwayFromZero));
Debug.Log (Math.Round(-1.5f, MidpointRounding.AwayFromZero));
Debug.Log (Math.Round(-0.5f, MidpointRounding.AwayFromZero));
Debug.Log (Math.Round(0.5f, MidpointRounding.AwayFromZero));
Debug.Log (Math.Round(1.5f, MidpointRounding.AwayFromZero));
Debug.Log (Math.Round(2.5f, MidpointRounding.AwayFromZero));
Debug.Log (Math.Round(3.5f, MidpointRounding.AwayFromZero));
```

I do get the expected results -4,-3,-2,-1,1,2,3,4.

How do you know the numbers you are rounding are exactly .5 ?
Common mistake is to assume that if `Debug.Log(someFloat);`

prints "0.5", that's the exact value of the float when in fact Debug.Log already rounds the numbers for you to be more readable.

EDIT. Oh u did indeed get the expected results as well... so just your expectations were wrong :)

I used this code:

```
float[] numbers = { -3.5f, -2.5f, -1.5f, -0.5f, 0.5f, 1.5f, 2.5f, 3.5f };
for ( int i = 0; i < numbers.Length; i++ )
{
float number = numbers[ i ];
Debug.Log ( ": " + number + "Default: " + Math.Round ( number ) + "AwayFromZero: " + Math.Round ( number, MidpointRounding.AwayFromZero ) );
}
```

I guess my expectation is wrong, but that is my question. How do you round to the nearest largest number if data ends with 0.5.

**Answer** by Jeff-Kesselman
·
Dec 16, 2014 at 08:22 PM

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);
}
```

MidpointRounding only has 2 options so if you @Konstantin$$anonymous$$us want to round always to the greater number, The quickest way would just be to make your own rounding function

```
using System;
public class MyMath {
public static int Round(float a)
{
if (a < 0) a += 0.5f;
return (int)Math.Round(a, MidpointRounding.AwayFromZero);
}
}
```

thanks for your reply, but here is the problem: i have value -0.9444. If add 0.5 to it, it will round to 0, which it should round to -1. So, that won't work. I think I have to check for 0.5 specifically.

your not being clear then as your expected examples above always rounded up, but now you say THIS case needs to round down.

State *exactly* what your rules for rounding are and we'll tell you how to do it.

Are you saying you always want to round +/-0.5 up but otherwise round to the closest up or down?

Thats a mathematically bizzare (and probably unjustifiable) function but if thats what you want we can ry to figure out how to do it. But you are right, since in that case -.5 acts in a way different from all other numbers it probably has to be special cased.

**Answer** by Eric5h5
·
Dec 16, 2014 at 08:30 PM

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.

**Answer** by tanoshimi
·
Dec 16, 2014 at 08:50 PM

Sounds like you want Mathf.Ceil.

Ah you're right, thats even simpler.

*bow*

Edit: Oh wait, actually no he doesnt

Ceil will ALWAYS give the higher number.

Eg Ciel(1.1) = 2

What he wants is

F(1.1) = 1

F(1.5) = 2

F(-1.5) = -1

Ergo my answer

wouldn't this round 0.4 to 1, which should be 0?

Well, this worked for all the test cases in your original question :)

**Answer** by KonstantinMaximus
·
Dec 16, 2014 at 10:58 PM

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!

### 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.

### Follow this Question

### Related Questions

unity Vector2 problem 1 Answer

How to make a button with rounded corners? 1 Answer

Getting the same 'rounded' value of a Vector3? 3 Answers

Rounding values for GUI 1 Answer

Is there a setting to change integer rounding to 'Swedish Rounding'? 2 Answers