Home >Web Front-end >JS Tutorial >Convert js numbers to float, take N decimal places_javascript skills
Convert js numbers to float, take N decimal places:
==========================================
Variables in JavaScript are weakly typed. All variables are declared as var. The type conversion process is not as convenient as Java. It performs type conversion through parseInt (variable), parseFloat (variable) and other methods. . Note: There is no type conversion like parseDouble (variable), because there is no distinction between single-precision float and double-precision double in JavaScript. All variables with decimals are considered floats, so n digits after the decimal must be taken. , you need to use the method toFixed(n) to get it.
For example:
var f=2.56556;
f.toFixed(2);
The 2 decimal digits after the variable f can be obtained. If it is not a float type, type conversion (parseFloat (variable name)) is required to obtain it through this method.