Javascript 为什么 Firebug 说 toFixed() 不是函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14059201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:36:42  来源:igfitidea点击:

Why does Firebug say toFixed() is not a function?

javascript

提问by Evik James

I am using jQuery 1.7.2 and jQuery UI 1.9.1. I am using the code below within a slider. (http://jqueryui.com/slider/)

我使用 jQuery 1.7.2 和 jQuery UI 1.9.1。我在滑块中使用下面的代码。(http://jqueryui.com/slider/)

I have a function that should test two values and depending on the difference between the two values reformat them (to the appropriate decimal place). If the difference is greater than 10, I will parse out the integer. If the difference is greater than 5, it should keep one decimal. Everything else, I will keep two decimals.

我有一个函数应该测试两个值,并根据两个值之间的差异重新格式化它们(到适当的小数位)。如果差值大于10,我会解析出整数。如果差值大于5,则保留一位小数。其他的,我会保留两位小数。

When I enter two values that have a difference that is ten or less, I use the toFixed() function. And, in Firebug, I see an error:

当我输入两个相差不超过 10 的值时,我使用 toFixed() 函数。而且,在 Firebug 中,我看到一个错误:

TypeError: Low.toFixed is not a function
Low = Low.toFixed(2);

Is there something simple that I am doing wrong?

有什么简单的事情我做错了吗?

Here is my code:

这是我的代码:

var Low = $SliderValFrom.val(),
High = $SliderValTo.val();

// THE NUMBER IS VALID
if (isNaN(Low) == false && isNaN(High) == false) {
    Diff = High - Low;
if (Diff > 10) {
      Low = parseInt(Low);  
  High = parseInt(High);    
} else if (Diff > 5) {
       Low = Low.toFixed(1);
       High = High.toFixed(1);
} else {
       Low = Low.toFixed(2);
   High = High.toFixed(2);
}
}

回答by jeremy

toFixedisn't a method of non-numeric variable types. In other words, Lowand Highcan't be fixed because when you get the value of something in Javascript, it automatically is set to a string type. Using parseFloat()(or parseInt()with a radix, if it's an integer) will allow you to convert different variable types to numbers which will enable the toFixed()function to work.

toFixed不是非数字变量类型的方法。换句话说,Low并且High无法修复,因为当您在 Javascript 中获取某些内容的值时,它会自动设置为字符串类型。使用parseFloat()(或parseInt()使用基数,如果它是整数)将允许您将不同的变量类型转换为数字,这将使toFixed()函数能够工作。

var Low  = parseFloat($SliderValFrom.val()),
    High = parseFloat($SliderValTo.val());

回答by Naftali aka Neal

That is because Lowis a string.

那是因为Low是一个字符串。

.toFixed()only works with a number.

.toFixed()只适用于一个数字。



Try doing:

尝试做:

Low = parseFloat(Low).toFixed(..);

回答by Jo?o Pimentel Ferreira

Lowis a string.

Low是一个字符串。

.toFixed()only works with a number.

.toFixed()只适用于一个数字。

A simple way to overcome such problem is to use type coercion:

克服此类问题的一个简单方法是使用类型强制

Low = (Low*1).toFixed(..);

The multiplication by 1 forces to code to convert the string to number and doesn't change the value. JSFiddle code here.

乘以 1 会强制编码将字符串转换为数字并且不会更改值。JSFiddle 代码在这里

回答by Idan

You need convert to number type:

您需要转换为数字类型:

(+Low).toFixed(2)

回答by Harry_pb

In a function, use as

在函数中,用作

render: function (args) {
    if (args.value != 0)
        return (parseFloat(args.value).toFixed(2));


},