D3.js的precisionRound()函數用於計算舍入為十進製表示法的小數點後的有效數字。它需要兩個參數step和max。
用法:
d3.precisionRound(step, max);
參數:它采用上述和以下所述的兩個參數:
- Step:它告訴需要格式化的兩個值之間的最小絕對差。
- Max:它表示需要格式化的最大絕對值。
返回值:它返回數字。
範例1:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
</head>
<body>
<!-- Fetching from CDN of D3.js -->
<script type="text/javascript"
src="https://d3js.org/d3.v4.min.js">
</script>
<script>
var p = d3.precisionRound(0.5, 1.5);
// For .5, 1.0, 1.5 the step should
// be 0.5 And the max should be 1.5
// so suggested precision is 2
console.log("Suggested precision is:", p)
var f = d3.format("." + p + "r");
console.log(f(.5));
console.log(f(1.0));
console.log(f(1.5));
</script>
</body>
</html>
輸出:
範例2:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
</head>
<body>
<!-- Fetching from CDN of D3.js -->
<script type="text/javascript"
src="https://d3js.org/d3.v4.min.js">
</script>
<script>
var p = d3.precisionRound(0.01, 1.01);
// For .55, .56,... the step should
// be 0.01 And the max should be 1.01
// so suggested precision is 3
console.log("Suggested precision is:", p)
var f = d3.format("." + p + "r");
console.log(f(.55));
console.log(f(0.56));
console.log(f(0.57));
console.log(f(.58));
console.log(f(.59));
</script>
</body>
</html>
輸出:
相關用法
- d3.js d3.map.set()用法及代碼示例
- p5.js box()用法及代碼示例
- p5.js nf()用法及代碼示例
- PHP Ds\Map get()用法及代碼示例
- PHP min( )用法及代碼示例
- PHP max( )用法及代碼示例
- PHP each()用法及代碼示例
- p5.js nfc()用法及代碼示例
- PHP key()用法及代碼示例
- PHP pos()用法及代碼示例
- d3.js d3.hsl()用法及代碼示例
- CSS var()用法及代碼示例
- PHP end()用法及代碼示例
- PHP next()用法及代碼示例
- PHP pi( )用法及代碼示例
注:本文由純淨天空篩選整理自tarun007大神的英文原創作品 D3.js precisionRound() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。