Format a string or number using Javascript

0

I have an application that consumes API . I have no control over that API , I can only consume it. The API returns in JSON format a name, latitude and longitude.

The problem is that due to a problem, the latitude and longitude returns it in an incorrect format:

  

[{"Name": "River   Side "," Latitude ": - 2524544568.0," Longitude ": - 5758220052.0}]

Obviously, I need Lat and Long to have the following format:

  

-25.245445680 and -57.582200520

Once I parked the JSON , and I have every data in a variable, how can I format the latitude and longitude using Javascript so that they are in the correct format?

Probe toFixed , but round the number.

    
asked by Guillermo Acosta 13.02.2017 в 12:24
source

2 answers

2

It is not clear to me what are the alternatives that the API returns, but, from what I gather, it would not work to remove all the points and then add a point after two digits from the right (considering that there may or may not be a sign)?

That is:

-2524544568.0 => -25245445680 => -25.245445680

In code:

var num = "-2524544568.0";
var res = num.toString().replace(".", "");
var pos = 2;
if(res[0] = '-'){
    pos = 3;
}
res = res.slice(0,pos) + "." + res.slice(pos);
document.getElementById("body").innerHTML = res;

Fiddle: link

    
answered by 13.02.2017 / 14:01
source
3

I think it would be enough to multiply the latitude and longitude by 0.00000001.

    
answered by 13.02.2017 в 12:45