Thursday, November 13, 2025
HomeLanguagesJavascriptD3.js precisionPrefix() Function

D3.js precisionPrefix() Function

The precisionPrefix() function of D3.js is used to return a specified precision, in turn, to be used with locale.formatPrefix.

Syntax:

d3.precisionPrefix(step, value);

Parameters: It takes the two parameters given above and described below.

  • Step: It tells the minimum difference between the values that are required to be formatted.
  • Value: Value here tells that what SI prefix is to be used with the given number e.g M for million or G for Giga.

Returns: It returns the number.

Below given are a few examples of the above function.

Example 1: Here p is 4 that means 4 digits will be there after the decimal.




<!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>
    // Here p is 4 that means for digits after decimal
    var p = d3.precisionPrefix(1e5, 1.2e10);
    let f = d3.formatPrefix("." + p, 1.2e10);
    let roundedNumber=f( 1.2e12);
    // Number of digits after decimal
    console.log("Value of p is: ", p);
    console.log("Type of p is: ", typeof p)
    // Old number is 
    console.log("Old number is: ", 1.2e12);
    // Number after the use of the precisionprefix
    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>
    var p = d3.precisionPrefix(1e2, 4.1e4);
    var f = d3.formatPrefix("." + p, 4.e1);
    var newNumber=f(4.85e10);
    // Number of digits after decimal
    console.log("Value of p is: ", p);
    // Old number is 
    console.log("Old number is: ", 4.85e10);
    // Number after the use of the precisionprefix
    console.log("New number is: ", newNumber);
    console.log("\n");
      
    var p = d3.precisionPrefix(1e2, 4.1e4);
    var f = d3.formatPrefix("." + p, 4.e5);
    var newNumber=f(4.85e10);
    // Number of digits after decimal
    console.log("Value of p is: ", p);
    // Old number is 
    console.log("Old number is: ", 4.85e10);
    // Number after the use of the precisionprefix
    console.log("New number is: ", newNumber);
    console.log("\n");
      
    var p = d3.precisionPrefix(1e4, 4.1e14);
    var f = d3.formatPrefix("." + p, 4.e10);
    var newNumber=f(4.85e10);
    // Number of digits after decimal
    console.log("Value of p is: ", p);
    // Old number is 
    console.log("Old number is: ", 4.85e10);
    // Number after the use of the precisionprefix
    console.log("New number is: ", newNumber);
    console.log("\n");
  </script>
</body>
</html>


Output:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11916 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11984 POSTS0 COMMENTS
Shaida Kate Naidoo
6889 POSTS0 COMMENTS
Ted Musemwa
7141 POSTS0 COMMENTS
Thapelo Manthata
6837 POSTS0 COMMENTS
Umr Jansen
6839 POSTS0 COMMENTS