# Why is there no Mathf.Truncate?

I found it odd that there is no Mathf.Truncate function. I am working with floats in formulas and need to Floor positive floats while Ceiling negative floats. Essentially this is rounding a float towards zero. Also, you can do the opposite by using MidpointRounding.AwayFromZero but this seems to be missing in Unity’s C# unless I’m doing something wrong.

Obviously, I can just use System.Math.Truncate but I have to convert my float equation into a double then cast truncate return to an int. This also makes me wish there was a TruncateToInt method as well.

Is there any reason for this?

Reference: System.Math.Truncate

In c# casting to int does that. Note that this is not common practice in all languages. In Java (not JavaScript) for instance the procedure is to always floor. In c++ it varies depending on OS/processor afaik both Intel and AMD Truncates which is probably also why MS decided to do the same for c#.

``````    value  truncate  cast  floor  ceil
0 :  2.1	   2	   2     2      3
1 :  1.9	   1	   1     1      2
2 :  0.1	   0	   0     0      1
3 :  0	     0	   0     0      0
4 : -0.1	   0	   0    -1      0
5 : -1.9	  -1	  -1    -2     -1
6 : -2.1	  -2	  -2    -3     -2
``````

run from this test

``````        [Test]
public void TestTruncate()
{
float[] numbers = {2.1f, 1.9f, 0.1f, 0.0f, -0.1f, -1.9f, -2.1f};
Debug.Log(\$"     value	truncate	cast	floor	ceil");
for (int i = 0; i < numbers.Length; i++)
{
float value = numbers*;*
``````

double truncate = Math.Truncate(value);
int cast = (int)(value);
int floor = Mathf.FloorToInt(value);
int ceil = Mathf.CeilToInt(value);
Debug.Log(\$“{i} : {value} {truncate} {cast} {floor} {ceil}”);
}
}