Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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"
Comment

JavaScript float precision 2

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Numbers</h1>
<h2>The toPrecision() Method</h2>

<p>toPrecision() formats a number to a specified length:</p>

<p id="demo"></p>

<script>
let num = 0.001658853;

document.getElementById("demo").innerHTML =
num.toPrecision(2) + "<br>" +
num.toPrecision(3) + "<br>" +
num.toPrecision(10);
</script>

</body>
</html>
Comment

PREVIOUS NEXT
Code Example
Javascript :: data fetch with axios 
Javascript :: download datepicker js 
Javascript :: mangoosejs 
Javascript :: pagination.js example codepen 
Javascript :: js difference between two arrays of objects 
Javascript :: background colour in react 
Javascript :: add parameter submit form javascript 
Javascript :: download pdf in javascript 
Javascript :: angular pass async pipe value to funciton 
Javascript :: search string javascript 
Javascript :: if statement in react native 
Javascript :: console.log 
Javascript :: jsp date 
Javascript :: how to set button width in javascript 
Javascript :: how to get the difference between two arrays in javascript 
Javascript :: event.propagation not working 
Javascript :: print string multiple times in javascript 
Javascript :: react functional components shortcut in webstorm 
Javascript :: regex char or char 
Javascript :: how to return argument in javascript 
Javascript :: module.exports multiple functions 
Javascript :: javascript window screen 
Javascript :: end session express 
Javascript :: material ui 
Javascript :: get text in protractor 
Javascript :: random trong js 
Javascript :: js get class from instance 
Javascript :: javascript read word document 
Javascript :: how to add abutton component to drawer in react native 
Javascript :: javascript string add new line 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =