How to Round Values in JavaScript using toFixed

You can round values in JavaScript using toFixed. Simply specify the number of decimal places needed within the brackets:

yourNumber.toFixed(number of decimal places needed)

Example of rounding to two decimal places using toFixed

Let’s say that you want to round the number 5.729 to two decimal places (hundredths). You can then round that value in JavaScript using toFixed:

5.729.toFixed(2);

You’ll get the result of 5.73

Calculator to round to 2 decimal places

This simple calculator would round your value to 2 decimal places:

Number:

Round to an Integer

If you want to round a value to an integer, simply place a ‘0’ within the toFixed brackets. You can then apply the following JavaScript code to round the number 5.729 to an integer:

5.729.toFixed(0);

And the result that you’ll get is 6

More Rounding Examples

Let’s now say that you have the number 7.29385672439. Depending on your needs, you can use toFixed to perform the rounding to your desired decimal places:

Round to Integer

7.29385672439.toFixed(0);

Round to 1 Decimal Place

7.29385672439.toFixed(1);

Round to 2 Decimal Places

7.29385672439.toFixed(2);

Round to 3 Decimal Places

7.29385672439.toFixed(3);

Round to 4 Decimal Places

7.29385672439.toFixed(4);

Round to 5 Decimal Places

7.29385672439.toFixed(5);