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" </ 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" </ 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: