**closed**Nov 06, 2018 at 04:22 PM by Kaideu for the following reason:

The question is answered, right answer was accepted

# Does this Truncate formula seem efficient?

So I've been scripting a game that uses a Stat system. I've been using mainly ints and a few floats for my numbers. Recently, I had to add some more floats and change some ints to floats. After fixing the compile errors associated with that mess, I found that I want to Truncate some of the floats. NOT ROUND, Truncate to 0 decimals. After an hour or 2 sifting through the forums for a truncate float method, I determined that I couldn't find one for float, only doubles or by some casting to int method that just seemed like too much. I decided to use the `Mathf.Round()`

method to create stand-in Truncate formula.

I was wondering if this was efficient enough to not have to do any double conversion or int casting and just use this for truncating floats from now on. `Mathf.Round(N - 0.5f)`

I don't see any problem with it so far, But I haven't used any absurdly finite floats or delved into the arbitrary math expressions realm with it yet.

**Answer** by DawidNorasDev
·
Nov 06, 2018 at 08:54 AM

You mean `Mathf.Floor(N);`

?

I guess my lack of knowledge precedes me after looking that up.

**Answer** by nurPunktMichi
·
Nov 06, 2018 at 09:44 AM

First, you are not truncating, but flooring (rounding down), which is different for negative numbers. Also, the Mathf.Round(N - 0.5f) won't even floor correctly. Math.Round() and Mathf.Round() have the quirk that they round exact midpoints to the even number, as documented. That means, that Mathf.Round(N - 0.5f) will return 2.f for 2.f, but 0.f for 1.f.

For actual truncation, I guess that the cast to int and back to float would be most efficient, actually.

I understand the negatives and didn't think about that since I won't be using them on this project.

However, for my positive numbers, I've seen the results I've been looking for and they seem to contradict what you're saying... Unless i'm misunderstanding you.

**Replacing N with MyFloat for readablilty**

From my tests:

```
public float MyFloat;
MyFloat = 1.93463f;
Debug.Log(Mathf.Round(MyFloat - 0.5f)); //Returns 1
MyFloat = 1.153453f;
Debug.Log(Mathf.Round(MyFloat - 0.5f)); //Returns 1
MyFloat = 0.92353f;
Debug.Log(Mathf.Round(MyFloat - 0.5f)); //Returns 0
MyFloat = 0.125353f;
Debug.Log(Mathf.Round(MyFloat - 0.5f)); //Returns 0
```

These seem to produce that I'm looking for every time, with the new exception of negatives.

So, I think I'm misunderstanding your statement?

Math.Round() and Mathf.Round() have the quirk that they round exact midpoints to the even number, as documented. That means, that Mathf.Round(N - 0.5f) will return 2.f for 2.f, but 0.f for 1.f

**EDIT Also, I understand that it's not true truncating. I guess I should've specified pseudo truncating?

**EDIT 2 After looking up the method given in the first answer, I understand my lack of knowledge. I could just replace my formula with the method `Mathf.Floor(MyFloat)`

It's exactly the same thing and I did not know of it beforehand

Yep, Mathf.Floor is mostly the same thing, except for the exact midpoints. Midpoint Rounding is what happens, when the parameter to Mathf.Round is exactly between two integers (1.5f, 2.5f etc.). With your formula, that happens when MyFloat has no fractional part, because you subtract 0.5f, so only for 0.f, 1.f, 2.f etc. Each even number will stay the same, but each uneven number will return the next lower integral. For System.Math.Round you have two options for midpoint rounding. In UnityEngine.Mathf.Round it's always "to even".

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