D3.js的precisionPrefix()函數用於返回指定的精度,該精度再與locale.formatPrefix一起使用。
用法:
d3.precisionPrefix(step, value);
參數:它采用上麵給定和下麵描述的兩個參數。
- Step:它告訴您需要格式化的值之間的最小差異。
- Value:此處的值表示要使用給定數字的SI前綴,例如M表示百萬,G表示千兆。
返回值:它返回數字。
下麵給出了上述函數的一些示例。
範例1:這裏的p是4,表示小數點後有4位數字。
<!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>
輸出:
範例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>
輸出:
相關用法
- d3.js d3.hsl()用法及代碼示例
- p5.js nf()用法及代碼示例
- p5.js nfc()用法及代碼示例
- p5.js nfp()用法及代碼示例
- PHP end()用法及代碼示例
- CSS var()用法及代碼示例
- PHP each()用法及代碼示例
- PHP key()用法及代碼示例
- d3.js zip()用法及代碼示例
- PHP pos()用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- p5.js box()用法及代碼示例
- p5.js nfs()用法及代碼示例
- PHP ord()用法及代碼示例
- p5.js mag()用法及代碼示例
注:本文由純淨天空篩選整理自tarun007大神的英文原創作品 D3.js precisionPrefix() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。