Js number to float N decimal places

  • 2020-03-30 01:38:17
  • OfStack

Js numeric conversion to float, take N decimal:

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Javascript variables are weakly typed, all variables are declared as var, in the type conversion process is not as convenient as Java, it is through parseInt(variable), parseFloat(variable) and other methods to type conversion. Note: there is no such type conversion as parseDouble(variable), because in javascript, there is no difference between single-precision float and double-precision double, all variables with decimals are considered to be floats, so to take the n bit after the decimal, you should use the method toFixed(n) to get.

Such as:
      Var f = 2.56556;
      F.t oFixed (2);

Take the 2 bits after the f decimal of the variable, and if it is not of float type, you need to cast (parseFloat(variable name)) to get it through this method.


Related articles: