在javascript中舍入计算的价格

时间:2016-11-15 09:45:43

标签: javascript rounding

我有点迷失在这里,我正在尝试构建一个表单来计算员工可以使用google maps api声明的成本。

一切似乎都运行正常,但我无法弄清楚如何将最多四舍五入。我希望有人能给我一些正确方向的提示

这是我的代码:

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

2 个答案:

答案 0 :(得分:5)

textarea { border: 1px; width: calc(100% -12px); /* plus the total left and right border */ padding: 5px; }

如果您已有浮动,则会返回parseFloat("123.456").toFixed(2);,您可以移除123.46

所以在你的例子中:

parseFloat()

答案 1 :(得分:0)

toFixed()函数返回一个字符串。如果你想要一个数字,再对结果应用parsefloat,如下所示

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