Math.round(num * 10) / 10
You can also use the toFixed() function.
var num = 2; var roundedString = num.toFixed(2);// 2.00
Simple example code.
<!doctype html> <head> <script> var number = 12.3456789 var rounded = Math.round(number * 10) / 10 console.log(rounded) </script> </head> <body> </body> </html>
if you want it to have one decimal place, even when that would be a 0, then add this
var fixed = rounded.toFixed(1) // fixed is always to 1 d.p. // NOTE: .toFixed() returns a string! // To convert back to number format parseFloat(number.toFixed(2)) // 12.34 // but that will not retain any trailing zeros
Do comment if you have any doubts or suggestions on this JS round code.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.