ToFixed, toexponential, toprecision in JS

ToFixed (): converts a number to a string according to the specified number of digits after the decimal point. It does not use exponential counting

Toexponential(): converts a number to an exponential string according to the number of digits specified after the decimal point. There is only one digit before the decimal point, and the number of digits after the decimal point is specified by parameters

Toprecision(): converts a number to a string based on the number of significant digits. If the number of significant digits is less than the integer part of the number, it is expressed as an exponential string

The above three methods all follow the principle of “rounding off” or filling 0

Here are some examples:

var num = 1234567.8999;
console.log("num.toFixed(0):" + num.toFixed(0));
console.log("num.toFixed(2)" + num.toFixed(2));
console.log("num.toFixed(5)" + num.toFixed(5));
console.log("num.toFixed(12)" + num.toFixed(12));

console.log("num.toExponential(3):" + num.toExponential(3));
console.log("num.toExponential(0):" + num.toExponential(0));

console.log("num.toPrecision(1):" + num.toPrecision(1));
console.log("num.toPrecision(5)" + num.toPrecision(5));
console.log("num.toPrecision(12)" + num.toPrecision(12));

Corresponding to the following output:

Similar Posts: