How to remove the decimals from a float without limiting it to ints max number.

I have numbers in the billions, that’s ok with floats but when I remove the decimal places I have to convert it to an int like so.

unRounded = unRounded / 100;
float rounded = (int)unRouned;
ub.cost = rouned * 100;

but that limits my numbers to the limit of int, instead of float.

Any way I can do this while keeping floats max number?

If it’s just for display, convert the float to a string and split the sting at the ‘.’ and show use the first element/string.

Or even just use a float format specifier to specify getting a string with no decimal places.

float rounded = UnityEngine.Mathf.Round(unrounded);

Or if you want to do it manually, do it like this:
float rounded = unrounded % 1f;

5 Likes

I used mathf.ceil is that the same?

Mathf.ceil will round up.
Mathf.floor will round down.

Try System.Math.Round instead of Mathf.

What are you doing with the result. It will help us know the correct approach. If you’re just wanting to remove the decimal point for display the solution is totally different than wanting to do arithmetic with the result.

It’s ok, mathf.ceil seems to work.

Well it was the cost of upgrades in an idle game I’m working on. I increase the cost of the upgrade every time it’s bought, with mathf.pow. But it had to be only be whole 100 numbers, and couldn’t be say “456” so I had to round.

unRounded was equal to the cost after using mathf.pow. This added some numbers to it that I didn’t want like “56” when I just wanted 500.

So I did the rounding but after getting into billions that stopped working…

It’s all good now though, mathf.ceil works. And mathf.floor when I want it to be the lowest.

That’s guys :stuck_out_tongue:

1 Like