Answers for "float precision, javascript"

1

float js precision

// JavaScript uses the 64-bit IEEE-754 floating point standard for storing numbers
const num = 314.15926535;
// toFixed(n) will convert a float to a string with n digits after the decimal point
num.toFixed() // "314" (same as num.toFixed(0))
num.toFixed(2) // "314.16"
num.toFixed(6) // "314.159265"
// toPrecision(n) will convert a float to a string with n digits total
num.toPrecision() // "314.15926535" (same as input)
num.toPrecision(2) // "3.1e+2" (sometimes will be in scientific notation)
num.toPrecision(6) // "314.159"
Posted by: Guest on May-30-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language