【问题标题】:toFixed method doesn't work on user input but works on other vars in javascript, snippet includedtoFixed 方法不适用于用户输入,但适用于 javascript 中的其他变量,包括片段
【发布时间】:2016-01-27 22:27:49
【问题描述】:

我想要反馈为什么当附加到用户输入 var usergb 时 toFixed 在我的 JS 警报中不起作用。是一件很奇怪的事情。有人鼓励我发帖,但我在这里没有看到任何其他关于用户输入变量的 toFixed 问题的问题。

var downloadspeed = function () {
        var usergb = prompt("How many gigabytes do you wish to download at 56 kilobits per second?");
        console.log(usergb);
        var hours = (usergb*8388608)/201600;
        console.log(hours);
        var days = (usergb*8388608)/4838400;
        console.log(days);

    //below in the alert is where you will see it, after the first concatenated string, I'd like to type usergb.toFixed(2) but it won't take.


        alert("The number of hours it will take to download a " + usergb + " GB file at 56 kilobits per second is " + hours.toFixed(2) + " hours. This might be a big number. So, in terms of days to download, it'll take "  + days.toFixed(2) + " days to download. This is why we have faster broadband speeds nowadays and why we have video streaming.");

    }
    downloadspeed();

我得到的错误是它不是一个函数。

格雷西,卢

【问题讨论】:

  • 我刚刚在我的浏览器中运行了那个确切的代码,它工作正常。
  • @MikeC - 我认为,通过阅读代码中的注释,问题在于在警报中将toFixed(2) 添加到usergb
  • @JaromandaX 你是对的。我错过了。

标签: javascript jquery methods


【解决方案1】:

toFixed 是一个数字上的方法...

prompt 返回一个字符串

试试parseFloat(usergb).toFixed(2)

【讨论】:

  • 谢谢,有道理!另一位用户发布了使用 +prompt("blah");看起来也一样,有什么不同的想法吗?
  • 没有想法。实际上我更喜欢这种方法
【解决方案2】:

你必须在号码上使用toFixed()

parseFloat(usergb).toFixed(2)

提示函数返回一个字符串,不能对字符串使用toFixed(),只能对数字使用。

【讨论】:

  • 您的答案包含与之前发布的相同的信息。
【解决方案3】:

您需要将输入转换为数字:

var usergb = +prompt("How many gigabytes do you wish to download at 56 kilobits per second?");

【讨论】:

  • 新东西让我学习,意思就是每当我提示时,只要在它前面加一个+,它就只接受输入的数字?还有其他像这样工作的古怪插件吗?谢谢!
  • @LouDiMe 不完全是。 prompt 总是返回一个字符串。 + 将字符串转换为数字。
【解决方案4】:

您可以解析 Number 类型的输入。 Example plunkr

var downloadspeed = function() {
  var input = prompt("How many gigabytes do you wish to download at 56 kilobits per second?");

  var usergb = new Number(input);
  if(isNaN(usergb))
  {
    alert("You must input a number");
    return;
  }

  console.log(usergb);
  var hours = (usergb * 8388608) / 201600;
  console.log(hours);
  var days = (usergb * 8388608) / 4838400;
  console.log(days);

  //below in the alert is where you will see it, after the first concatenated string, I'd like to type usergb.toFixed(2) but it won't take.


  alert("The number of hours it will take to download a " + usergb + " GB file at 56 kilobits per second is " + hours.toFixed(2) + " hours. This might be a big number. So, in terms of days to download, it'll take " + days.toFixed(2) + " days to download. This is why we have faster broadband speeds nowadays and why we have video streaming.");

}
downloadspeed();

【讨论】:

  • 尝试插入您的代码但没有通过,我是否遗漏了您插入的内容?
  • 不确定你的意思。你去示例 plunkr 了吗?你用什么浏览器测试?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-15
  • 1970-01-01
  • 2021-01-16
  • 1970-01-01
  • 1970-01-01
  • 2018-12-07
相关资源
最近更新 更多