Forum Moderators: open

Message Too Old, No Replies

Rounding the distance to single number

Gmap give varchar 12.3 mi need 12 or 13 back

         

cchapman09

7:30 pm on Dec 22, 2008 (gmt 0)

10+ Year Member


I'm using javascript to get the distance from Google maps. I get back 12.3 mi and I need it to display as 12 or 13. Is there a way to add this function to my code below.

<script type="text/javascript">
//<![CDATA[
google.load("maps", "2");
var gdir;
function load() {
if (GBrowserIsCompatible()) {
gdir = new google.maps.Directions();
google.maps.Event.addListener(gdir, "load", handleLoad);
gdir.load("from: '+&P8_FROMADDRESS.+' to: '+&P8_TOADDRESS.", {getSteps: true});
}
}
function handleLoad() {
document.getElementById("totalMiles").innerHTML = gdir.getDistance().html;
}
window.onload = load;
window.onunload = google.maps.Unload;
//]]>
</script>

g1smd

8:21 pm on Dec 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Do you need to round it to the nearest whole number, or just strip the digits after the decimal point (where 15.99 would become 15)?

cchapman09

10:27 pm on Dec 22, 2008 (gmt 0)

10+ Year Member



I would like to round it, but stripping the digits after the decimal point will be good.

phranque

2:43 am on Dec 23, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], cchapman09!

you could use the round() method of the Math object [w3schools.com]

rocknbil

3:47 pm on Dec 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use Math.round() to int() to the nearest next integer, (down under .5, up at .5 or more,) Math.ceil() [w3schools.com] to int() up to the next integer, Math.floor() [w3schools.com] to round down.