admin管理员组

文章数量:1134555

I am using jQuery 1.7.2 and jQuery UI 1.9.1. I am using the code below within a 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.

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:

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);
}
}

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/)

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.

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:

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);
}
}
Share Improve this question edited Dec 27, 2012 at 18:16 Naftali 146k41 gold badges247 silver badges304 bronze badges asked Dec 27, 2012 at 18:12 Evik JamesEvik James 10.5k19 gold badges75 silver badges123 bronze badges 1
  • I have same problem. but in local its work. when in live it show an error - TypeError: value.sell_price.toFixed is not a function – Mizanur Rahman Commented Dec 1, 2019 at 17:19
Add a comment  | 

7 Answers 7

Reset to default 199

toFixed isn't a method of non-numeric variable types. In other words, Low and High can'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.

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

That is because Low is a string.

.toFixed() only works with a number.


Try doing:

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

Low is a string.

.toFixed() only works with a number.

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.

You need convert to number type:

(+Low).toFixed(2)
Low = Number(Low).toFixed(1);

add the Number function to convert Low into a number.

parseFloat() will return NaN for empty string, why not using Number() function instead?

Values of other types can be converted to numbers using the Number() function.

parseFloat('').toFixed(2) // "NaN"
Number('').toFixed(2) // "0.00"

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number

In a function, use as

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


},

本文标签: javascriptWhy does Firebug say toFixed() is not a functionStack Overflow