0

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>
bprb
  • 11
  • 3
  • Welcome to StackOverflow. Does this help? http://stackoverflow.com/questions/15762768/javascript-math-round-to-two-decimal-places – José Luis Nov 15 '16 at 09:52

2 Answers2

5

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);

        }
    });         
}
Shaun Parsons
  • 362
  • 1
  • 11
  • Thanks Shaun! this was my first guess aswell. Where would i put it since it does not seem to work for me. – bprb Nov 15 '16 at 10:11
  • Answer updated. – Shaun Parsons Nov 15 '16 at 10:24
  • It works Shaun thank you so much for your time and help, 1 little edit: 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; price.value = parseFloat(price.value).toFixed(2); console.log(price.value); } }); } – bprb Nov 15 '16 at 11:49
  • Not a problem. I've tidied up my answer for you to remove the `console.log` and you can do `price.value = parseFloat(distanceInput.value * 0.19).toFixed(2);` just to tidy it up a bit. – Shaun Parsons Nov 15 '16 at 11:56
  • Your the best, thanks thanks a lot! – bprb Nov 18 '16 at 09:26
0

toFixed() function returns a string.If you want a number, apply parsefloat again on the result, like below

var a = "5.57849" ; a = parseFloat(parseFloat(a).toFixed(2)) ;

Prashanth
  • 21
  • 4