# Round to decimals

 0 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. more ▼ asked Dec 20 '11 at 02:47 PM uberwleiss 198 ● 10 ● 14 ● 22 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 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. more ▼ answered Dec 20 '11 at 02:52 PM OrangeLightning 5.4k ● 47 ● 57 ● 113 Thanx! Haven't thought of that. Dec 20 '11 at 03:18 PM uberwleiss add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x24
x24
x5

asked: Dec 20 '11 at 02:47 PM

Seen: 1540 times

Last Updated: Dec 20 '11 at 03:18 PM