How to round to lower

Hello, i need a script to round like 1234 coins to 1,2k coins. I already made it - CoinsK = (Mathf.Round(CoinsAmount) / 1000).ToString("0.0") + "K";

But the problem is that if coins value is like 1290 it rounds it to 1,3k but i need it to round the value to the lower - 1,2k (idk how to say it in english :smile:). Because if something costs 1,5k and player got 1490 coins that is rounded to 1,5k it would confuse the player.

I tried to do it with Mathf.Floor but the result is the same i guess, because of this i think - .ToString**(“0.0”)**

Is there a way to do it? It there is please help. Thank you.

Check the parenthesis. You need to floor after you’ve divided by 1000.

1 Like

Wouldn’t that round to the lowest 1000th? The op wants to round to the lowest 100th.

float calc = (float)CoinsAmount / 100f;
calc = Mathf.Floor( calc );
CoinsK = ( calc / 10f ).ToString("0.0") + "K";

Or if you like one-liners :

CoinsK = ( Mathf.Floor( (float)CoinsAmount / 100f ) / 10f ).ToString("0.0") + "K";
3 Likes

Thank you, it works. But what means that (float) in there? Why i need it?

the (float) is a cast, it is just converting the int CoinsAmount to a float before it divides by 100f

1 Like

It’s not actually needed as you have the / 100f which means the 100 is a float rather than an int.

Essentially if you divide 2 ints you get an int (and thus no decimal places), if you divide an int and a float, a float and an int or a float and a float you get a float (and thus you have decimal places).

1 Like