I have the below in a google map application and want to display the elevation converted to feet but how do I round up/down to the nearest number? (eliminate digits after the decimal) I tried the number.toFixed(x) method but nothing seemed to do it.
function getElevation(event) {
var locations = [];
var clickedLocation = event.latLng;
locations.push(clickedLocation);
var positionalRequest = { 'locations': locations }
// Initiate the location request
elevator.getElevationForLocations(positionalRequest, function(results, status) {
if (status == google.maps.ElevationStatus.OK) {
// Retrieve the first result
if (results[0]) {
// Open an info window indicating the elevation at the clicked position
infowindow.setContent("The elevation at this point <br/>is " + results[0].elevation*(3.2808399) + " feet.");
infowindow.setPosition(clickedLocation);
infowindow.open(map);
} else {
alert("No results found");
}
} else {
alert("Elevation service failed due to: " + status);
}
});
}
If you want to round to the nearest integer, simply use
Math.round(x)You may also want to look into
Math.floor(always rounds down), andMath.ceil(always rounds up).I’ve put together a Fiddle that demonstrates all three methods: jsFiddle