how do i round a float 1.548 to 1.54?

how do i round a float 0.000 to 0.00.
the number is random.

so sometimes (for example):
round a float 3.236 to 3.23 or
round a float 5.236 to 5.23
basically remove the third number after the period

All examples you have shown does not round correctly. When you round to two decimal places then 1.548 would round to 1.55 and 3.236 would round to 3.24 and 5.236 would round to 5.24. If you actually want to get those values you mentioned you don’t seem like you want to round the values, but to truncate after two decimal places.

Anyways the idea is always the same. When you want to round to 2 decimal places:

  • multiply the number by 100
  • round / floor the number
  • divide by 100 again.

In case of 1.548 when you multiply by 100 you get 154.8. Now you can use Mathf.Floor or Mathf.Round (depending on what behaviour you want) to get either 155 or 154. Finally divide by 100 to get back the original magnitude: 1.55 or 1.54

This should help.

float value = whateverNumberYouAreTryingToGetDecimalFor
Math.Round(value, 2);

I think that is how it should work for what you are doing.

Edit after I found flaw:

double value = new double();
value = Math.Round(whateverNumberYouAreTryingToGetDecimalFor, 2);