D3.js precisionFixed() Function

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" </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" </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:
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek 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!




