Questions tagged [parsefloat]

parseFloat is a global JavaScript method which parses an argument, and returns a number.

JavaScript's parseFloat attempts to parse the first argument, and returns a number. If the argument cannot be parsed, parseFloat returns NaN.

A comparison of number conversion methods can be found at this answer.

222 questions
9
votes
3 answers

Javascript parseFloat and nulls

I am very new to javascript as I am currently making a cross platform web app in jQuery Mobile, I have used the example of XML Parsing to a HighCharts graph yet when I encounter a null in my series data it fails to draw any of the line and makes it…
Palendrone
  • 363
  • 1
  • 2
  • 14
7
votes
3 answers

Javascript multiplying incorrectly, causing incorrect rounding

When I pull the values I want to multiply, they're strings. So I pull them, parse them as floats (to preserve the decimal places), and multiply them together. LineTaxRate = parseFloat(myRate) * parseFloat(myQuantity) *…
matt anderson
  • 81
  • 1
  • 3
7
votes
6 answers

Passing a number to parseFloat() instead of string

In my code, the value of a particular var can originate from any one of a number of different json sources. For some of those sources, the json element concerned will be a string (e.g. "temp": "10.2"), while for other sources the json element will…
drmrbrewer
  • 11,491
  • 21
  • 85
  • 181
6
votes
1 answer

Are there are any side effects of using this method to convert a string to an integer

Are there any side effects if i convert a string to a number like below.. var numb=str*1; If I check with the below code it says this is a number.. var str="123"; str=str*1; if(!isNaN(str)) { alert('Hello'); } Please let me know if there…
Exception
  • 8,111
  • 22
  • 85
  • 136
6
votes
3 answers

Does parseFloat ever throw an error?

In the codebase I'm working on, I encountered code like this: try { price = parseFloat(price); } catch (err) { console.log(err); } I know that in most cases where price cannot be turned into a number, it'll simply get the value of NaN…
Bart S
  • 1,698
  • 1
  • 16
  • 21
5
votes
2 answers

Why parseFloat in javascript returns string type for me?

I searched and only found this one related to my question, but not exactly the same, as I'm using toFixed rather than toPrecision. Why does toPrecision return a String? Here is my code var oldv =…
Tony Xu
  • 3,031
  • 4
  • 32
  • 43
5
votes
4 answers

Convert string to either integer or float in javascript

Is there a straightforward way to parse a string to either integer or float if I don't know if the variable is integer-number-like or decimal-number-like? a = '2'; // => parse to integer b = '2.1'; // => parse to float c = '2.0'; // => parse to…
am2124429
  • 425
  • 2
  • 6
  • 8
5
votes
0 answers

parseFloat method not giving proper output

Here is a sample program: var a = parseFloat("86.69043189999999") + "
"; var b = parseFloat("86.79718199999999") + "
"; var n = a + b; document.getElementById("demo").innerHTML = n;

Both the two variables have exact same…
Anurag Chakraborty
  • 421
  • 2
  • 5
  • 20
5
votes
6 answers

orderBy number angular js

I have an array of objects... var userObjects =[ {userName: bob, age: "25", gender: "m" }, {userName: bill, age: "15", gender: "m" }, {userName: jen, age: "45", gender: "f" }, ] Now I have an ng-repeat in my HTML to iterate over this array of…
user1876246
  • 1,219
  • 5
  • 18
  • 33
4
votes
1 answer

Does a hardcode float num a.bc... (0<=n<100 decimals,MIN_VALUE<=a.bc...<=MAX_VALUE) always equals to Number.parseFloat((a.bc...).toFixed(n))?

For example, I know 0.12,ie: const a=0.12; rounds to something due to float number errors. However, I'm not asking why the error happens and I don't care the errors currently, instead I want to know if the hardcode form always equals to the value…
3
votes
1 answer

How can I implement v-model.number on my own in VueJS?

I have a text field component for numeric inputs. Basically I'm just wrapping v-text-field but in preparation for implementing it myself. It looks like this.