- Home /
C# Round either up or down at .5
Why the hell does the round function go to the nearest even number!? I feel like I'm going crazy, or must be missing something, but as far as I can find there's no function to just either round up or down at .5. There's fairly easy workarounds and I've got it to do what I need to do but this is baffling to me. Why is this a thing?! Someone please explain. I feel like I must be missing something if I can't find an answer to this already. Hopefully I'm not just too tired to realize at the minute. Also not trying to be mad or anything here, just really can't stress enough how much this confuses me.
maybe refrase your question. Isn't rounding in any interpretation "going to the nearest even number"? There is $$anonymous$$athf.Abs()
wich just cuts of the uneven stuff so its always rounding down.
Absolute makes it a positive number. I meant that it will with lets say 15.5 round it either up to 16 or down to 14 ins$$anonymous$$d of round it to the nearest even number of 16, since that means 16.5 would also be rounded to 16.
No, your examples are wrong. $$anonymous$$ath.Round ($$anonymous$$athf.Round is just a wrapper) always rounds to the nearest integer. The only exeption is exactly at .5. Any .5 value is always betwen an odd and an even number. The toeven convention simply picks the even number in case of ".5". However any other value so ".50000001" will always round up and ".4999999" will always round down the the nearest integer. So a 15.5 can never become a 14. Any value that is larger than 14.5 and smaller than 15.5 will round to 15 any value larger than 15.5 and smaller than 16.5 will round to 16.
The "toeven" rule only affects the exact value of "15.5". It's a value between 15 and 16. 15 is odd, 16 is even so it will pick 16. However 14.5 is between 14 and 15. 14 is even, 15 is odd so it will pick 14
input --> $$anonymous$$ath.Round(input)
// ----------------
0.4999999 --> 0
0.5 --> 0
0.5000001 --> 1
1.4999999 --> 1
1.5 --> 2
1.5000001 --> 2
2.4999999 --> 2
2.5 --> 2
2.5000001 --> 3
3.4999999 --> 3
3.5 --> 4
3.5000001 --> 4
4.4999999 --> 4
4.5 --> 4
4.5000001 --> 5
5.4999999 --> 5
5.5 --> 6
5.5000001 --> 6
i think you might be doing something wrong. i just tested this as working from microsoft's math library:
int rounded = (int)($$anonymous$$ath.Round(15.4f));
print("rounded:" + rounded);//prints 15
rounded = (int)($$anonymous$$ath.Round(15.6f));
print("rounded:" + rounded);//prints 16
also i have never had problems with mathf functions in unity either. post your code if you are having problems
The "toeven" convention only applies to exact ".5" values. Since .5 is exactly in between two integer numbers there is no general or logical way how you should round the value. The toeven convention simply picks the even number. Try rounding the values 1.5f and 2.5f and 3.5f and you will see the difference.
Answer by Meguia · Feb 16, 2018 at 02:07 AM
Hi @Lukephos !
The behavior of Unity's Mathf.Round()
is the same as the default C# version Math.Round()
. As explained here, the default C# behavior uses ToEven
convention.
In C# you can use MidpointRounding enum as a parameter of Math.Round()
to choose it's behavior but I think you can't do the same with Unity's Mathf.Round()
¯\_(ツ)_/¯
Answer by ElijahShadbolt · Feb 16, 2018 at 08:00 PM
public static float MyRound(float value) {
if (value % 0.5f == 0)
return Mathf.Ceil(value);
else
return Mathf.Floor(value);
}
Example Output:
MyRound(5.5f) => 6
MyRound(4.5f) => 5
MyRound(0.5f) => 1
MyRound(-0.5f) => 0
MyRound(-1.5f) => -1
MyRound(4.0f) => 4
MyRound(3.0f) => 3
MyRound(3.4999f) => 3
Your answer
![](https://koobas.hobune.stream/wayback/20220612152721im_/https://answers.unity.com/themes/thub/images/avi.jpg)