bprb bprb - 19 days ago 6
Javascript Question

Rounding a calculated price in javascript

I'am a little bit lost here, i am trying to build a form that will calculate the costs a employee can declarate using the google maps api.

Everything seems to work fine but i can't figure out how to round up to up to two decimals. I was hoping someone could give me some tips in the right direction

Here's my code:

<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<title>Distance Calculator</title>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCGt7NK298P8LYoxRumy7OHoL32xaQOWwQ&signed_in=true&libraries=places"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<style type="text/css">
#map_canvas {
height:500px;
}
</style>

<script type="text/javascript">
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;

function initialize() {
var start = document.getElementById("start");
var end = document.getElementById("end");
var autocompletePickpUp = new google.maps.places.Autocomplete(start);
var autocompleteDelivery = new google.maps.places.Autocomplete(end);
var uk = new google.maps.LatLng(52.216838, 4.433652);
var myOptions = {
zoom:12,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: uk
}

//testing


directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setMap(map);
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
autocompletePickpUp.bindTo('bounds', map);
autocompleteDelivery.bindTo('bounds', map);

}

function calcRoute() {
var startValue =start.value;
var endValue = end.value;
var distanceInput = document.getElementById("distanceKm");
var price = document.getElementById("price");


var request = {
origin:startValue,
destination:endValue,
travelMode:google.maps.DirectionsTravelMode.DRIVING
};

directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
distanceInput.value = response.routes[0].legs[0].distance.value / 1000;
price.value = distanceInput.value * 0.19;

}
});
}


</script>
</head>
<body onload="initialize()">
<div>
<p>
<label for="start">Vertrekpunt </label>
<input type="text" name="start" id="start" />

<label for="end">Bestemming </label>
<input type="text" name="end" id="end" />

<input type="submit" value="Bereken declaratie" onclick="calcRoute()" />
</p>
<p>
<label for="distanceInKm">Afstand (km): </label>
<input type="text" name="distanceKm" id="distanceKm" readonly="true" />
</p>
<p>
<label for="price">Te declareren </label>
<input type="text" name="price" id="price" readonly="true" />
</p>
</div>
<div id="map_canvas"></div>
</body>

Answer

parseFloat("123.456").toFixed(2);

Will return 123.46 if you already have a float you can remove the parseFloat()

So in your example:

function calcRoute() { 
    var startValue =start.value;
    var endValue = end.value;
    var distanceInput = document.getElementById("distanceKm");
    var price = document.getElementById("price");


    var request = {
        origin:startValue, 
        destination:endValue,
        travelMode:google.maps.DirectionsTravelMode.DRIVING
    };

    directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
            distanceInput.value = response.routes[0].legs[0].distance.value / 1000; 
            price.value = parseFloat(distanceInput.value * 0.19).toFixed(2);

        }
    });         
}
Comments