10

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",
0

3 Answers 3

18

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

Sign up to request clarification or add additional context in comments.

Comments

4

Convert it to a number: You'll use the parseFloat() or parseInt() methods.

parseFloat('28.14331') // 28.14331
parseInt('28.14331', 10) // 28

1 Comment

use radix always of base 10 if you use parseInt
0

you can use the parseFloat function to turn the string into a number

latitude = parseFloat(data8.weblandmarks8[j].latitude) + latitude;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.