Reputation:
I have a json file which I am accessing through JS
latitude =data8.weblandmarks8[j].latitude + latitude;
should add all the latitudes so that I could average them later instead it just concatenates them How should i achieve what I want to
Json entry
"latitude": "28.14331",
Upvotes: 10
Views: 45283
Reputation: 3165
Convert it to a number: You'll use the parseFloat()
or parseInt()
methods.
parseFloat('28.14331') // 28.14331
parseInt('28.14331', 10) // 28
Upvotes: 4
Reputation: 122906
Aside from using parseFloat
you could convert to Number
, it's slightly faster and usable for both integers and floats1:
latitude = Number(data8.weblandmarks8[j].latitude) + latitude;
1 another advantage for integers is that you don't need to supply a radix. Number('09')
returns 9, whereas parseInt('09')
(i.e. parseInt
without a radix) returns 0 (that's because parseInt
defaults to an octal - radix 8).
Upvotes: 18
Reputation: 43810
you can use the parseFloat function to turn the string into a number
latitude = parseFloat(data8.weblandmarks8[j].latitude) + latitude;
Upvotes: 0