Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

D3.js precisionFixed() Function

  • Last Updated : 29 Jul, 2020

The d3.precisionFixed() function in D3.js is used to return the decimal precision for the fixed floating point notation on the basis of the given step value.

Syntax:

d3.precisionFixed(step);

Parameters: It takes only one parameter that is given above and described below.

  • Step: Step value denotes how much digits are required after the decimal for e.g 0.1 means one digit after the decimal is in the output and 0.001 means 3 digits after the decimal.

Return Value: It returns a number.

Below given are a few examples for the above-given function.

Example 1:




<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" 
        content="width=device-width, 
                 initial-scale=1.0">
  <title>Document</title>
</head>
<style>
</style>
<body>
  <!--Fetching from CDN of D3.js -->
  <script type = "text/javascript"
          src = "https://d3js.org/d3.v4.min.js">
  </script>
  <script>
    let p=d3.precisionFixed(0.001);
    let f=d3.format("."+p+"f");
    let roundedNumber=f(.12359);
    console.log("Old number is: ", 0.012345);
    console.log("New number is: ", roundedNumber);
  </script>
</body>
</html>

Output:

Example 2:




<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" 
        content="width=device-width,
                 initial-scale=1.0">
  <title>Document</title>
</head>
<style>
</style>
<body>
  <!--Fetching from CDN of D3.js -->
  <script type = "text/javascript"
          src = "https://d3js.org/d3.v4.min.js">
  </script>
  <script>
    let p=d3.precisionFixed(0.005);
    let f=d3.format("."+p+"f");
    let roundedNumber=f(.21543);
    // Number of digits after decimal
    console.log("Value of p is: ", p);
    console.log("Type of p is: ", typeof p)
    console.log("Old number is: ", 0.21543);
    console.log("New number is: ", roundedNumber);
  
    console.log("\n");
    p=d3.precisionFixed(1);
    f=d3.format("."+p+"f");
    roundedNumber=f(2.21543);
    // Number of digits after decimal
    console.log("Value of p is: ", p);
    console.log("Old number is: ", 0.21543);
    console.log("New number is: ", roundedNumber);
  
      
    console.log("\n");
    p=d3.precisionFixed(0.01);
    f=d3.format("."+p+"f");
    roundedNumber=f(.21543);
    // Number of digits after decimal
    console.log("Value of p is: ", p);
    console.log("Old number is: ", 0.21543);
    console.log("New number is: ", roundedNumber);
  
      
    console.log("\n");
    p=d3.precisionFixed(0.000002);
    f=d3.format("."+p+"f");
    roundedNumber=f(.21543);
    // Number of digits after decimal
    console.log("Value of p is: ", p);
    console.log("Old number is: ", 0.21543);
    console.log("New number is: ", roundedNumber);
  </script>
</body>
</html>

Output:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!