Friday 25 November 2016

math - rounding numbers up in javascript having problems




hey i am having problems with rounding up numbers because Im not sure how i would go about doing it this is the code is use for the maths:




    var lon1 = position.coords.longitude* 0.0174532925;
var lat1 = position.coords.latitude * 0.0174532925;
var i = 0;

while (i {
x.innerHTML+= "
Distance " + calcDist(lon1, lat1, locations[i][1]* 0.0174532925, locations[i][2]* 0.0174532925);
i++;
}
}

function calcDist(lon1, lat1, lon2, lat2)
{
return Math.acos(Math.sin(lat1)*Math.sin(lat2) +
Math.cos(lat1)*Math.cos(lat2) *
Math.cos(lon2-lon1)) * 3958;
}


the reason i am asking is because i am creating a store locator and this is for the distance between the two points the user and the store but when it works it out it shows as 5.595255493978103 instead of 5.6 miles




any help would be much appreciated thanks in advance


Answer



// Round towards nearest multiple of 0.1
function round(x) {
return Math.round(x * 10) / 10;
}






x.innerHTML+= "
Distance " + round(calcDist(lon1, lat1, locations[i][1]* 0.0174532925, locations[i][2]* 0.0174532925));

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...