当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


d3.js precisionRound()用法及代码示例


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>

输出:




相关用法


注:本文由纯净天空筛选整理自tarun007大神的英文原创作品 D3.js precisionRound() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。