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

I am using jQuery 1.7.2 and jQuery UI 1.9.1. I use the code below in the slider. (Http://jqueryui.com/slider/)

I have a function that should check two values ​​and reformat them (to the appropriate decimal place) depending on the difference between the two values. If the difference is greater than 10, I will analyze the integer. If the difference is greater than 5, it must contain one decimal place. Everything else, I will keep two decimal places.

When I enter two values ​​that have a difference of 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 I'm 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); } } 
+69
javascript
Dec 27 '12 at 18:12
source share
5 answers

toFixed not a method of non-numeric variable types. In other words, Low and High cannot be fixed, because when you get the value of something in Javascript, it is automatically set to a string type. Using parseFloat() (or parseInt() with a radius if it is an integer), you can convert different types of variables into numbers that will allow you to work with the toFixed() function.

 var Low = parseFloat($SliderValFrom.val()), High = parseFloat($SliderValTo.val()); 
+131
Dec 27 '12 at 18:13
source share

This is because Low is a string.

.toFixed() only works with numbers.




Try to do:

 Low = parseFloat(Low).toFixed(..); 
+74
Dec 27 '12 at 18:13
source share

Low string.

.toFixed() only works with numbers.

An easy way to overcome this problem is to use type casting:

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

Multiplying by 1 causes the code to convert the string to a number and does not change the value. JSFiddle code is here .

+18
Nov 22 '16 at 20:47
source share

Low = parseFloat(Low).toFixed(..); works :) I learned it with difficulty.

+16
01 Sep '15 at 11:24
source share

In function use as

 render: function (args) { if (args.value != 0) return (parseFloat(args.value).toFixed(2)); }, 
0
Jan 15 '19 at 19:22
source share



All Articles