The toFixed() method returns a string representation of the value in the specified decimal place
var num = 10;
console.log(num.toFixed(2)); / / "10.00"
// The toFixed() method is passed the value 2, showing 2 decimal places
Copy the code
var num = 10.005;
console.log(num.toFixed(2)); / / "10.01"
// If the value itself contains more decimal places than specified, values close to the specified maximum number of decimal places are rounded
Copy the code
The toExponential() method returns a string representation of a value represented in exponential notation (also known as e notation).
var num = 10;
console.log(num.toExponential(1)); 1.0 e+1 "/ /"
// Specifies the number of decimal places in the output
Copy the code
The toPrecision() method may return either a fixed size format or an exponential format
var num = 99;
console.log(num.toPrecision(1)); // The "1e+2" digit does not accurately represent 99, and is rounded up to 100
console.log(num.toPrecision(2)); //"99" is a two-digit number
console.log(num.toPrecision(3)); //"99.0" is a three-digit number
Copy the code
In fact, toPrecision() will decide whether toFixed() or toExponential() should be called based on the value to be handled.