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 开发者_Go百科concatenates them How should i achieve what I want to
Json entry
"latitude": "28.14331",
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).
Convert it to a number: You'll use the parseFloat()
or parseInt()
methods.
parseFloat('28.14331') // 28.14331
parseInt('28.14331', 10) // 28
you can use the parseFloat function to turn the string into a number
latitude = parseFloat(data8.weblandmarks8[j].latitude) + latitude;
精彩评论