Syntax error How to round the decimal number to the nearest tenth in 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
Updated on: 2020-09-01T11:56:30+05:30

600 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements