Round to decimals

Hi,

i wonder if there is a function to round a number to a given number of decimals in javascript. For example, to round 2.543686 to 2.54.

The (10.8).toFixed(2); doesn’t work in unity, and .ToString(F2); is converting a number to a string.

Thanks.

To be completely honest I don’t know for sure. Maybe something like this:

    var randomNumber = 2.543686;

    randomNumber = Mathf.Round(randomNumber * 100.0) / 100.0;

This will round ‘randomNumber’ to two decimals.

If you want three decimals you can divide by 1000.0.

If you want four decimals you can divide by 10000.0.

And so on.

Check here

Answer

sharin’ is carin’

https://twitter.com/IndieNendoroid