Computer >> Computer tutorials >  >> Programming >> Javascript

How to round the decimal number to the nearest tenth in JavaScript?


To round the decimal number to the nearest tenth, use toFixed(1) in JavaScript. The syntax is as follows −

var anyVaribleName=yourVariableName.toFixed(1)

Let’s say the following is our decimal number −

var decimalValue =200.432144444555;
console.log("Actual value="+decimalValue)

Let’s now round the decimal number. Following is the code −

Example

var decimalValue =200.432144444555;
console.log("Actual value="+decimalValue)
var modifiedValue=decimalValue.toFixed(1)
console.log("Modified value="+ modifiedValue);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo38.js.

Output

This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo38.js
Actual value=200.432144444555
Modified value=200.4