- Home /
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.
Comment
Answer by FLASHDENMARK · Dec 20, 2011 at 02:52 PM
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.
Your answer
Follow this Question
Related Questions
Shown decimal places 2 Answers
How to round a number? 2 Answers
I dont want Decimals :P 1 Answer
Round Slider (Circle shaped slider GUI control) 1 Answer
C# round transform.position floats to .5 2 Answers