【问题标题】:How would I get this function to only return three numbers? [duplicate]我怎样才能让这个函数只返回三个数字? [复制]
【发布时间】:2020-05-19 00:43:10
【问题描述】:
**<script>
function roundNum() {
var f = document.getElementById("f").value;
var g = document.getElementById("g").value
var g = g-u
document.getElementById("ng").innerHTML = g
</script>**

我正在尝试接收三位数的答案(例如:95.7,而不是 95.6923531)

【问题讨论】:

  • 您可以使用.toFixed() 将数字转换为具有最大小数位数的字符串。您不能将实际数字限制为特定的小数位数,因为数字是 二进制 浮点数。
  • 当您遇到此类问题时,只需使用 google。您是第一个想要格式化数字的人的机会等于 0 ;) stackoverflow.com/questions/4098685/…

标签: javascript html


【解决方案1】:

您可以使用 javascript toFixed() 方法。

例子:

var x = 9.656;
x.toFixed(1);           // returns 9.7
x.toFixed(2);           // returns 9.66
x.toFixed(4);           // returns 9.6560
x.toFixed(6);           // returns 9.656000

【讨论】:

    【解决方案2】:

    试试这个..

    语法: number.toFixed(x)

    var num = 95.6923531;
    console.log(num.toFixed(1));
    console.log(num.toFixed(2));
    console.log(num.toFixed(3));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-13
      • 2021-10-30
      • 2016-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多