0

您好,我有里程计算器的代码,我希望显示的里程四舍五入到小数点后一位。这个代码是什么?

不要只说将 1609.3440006146921597227828997904 向上舍入,因为我希望计算出的里程在四舍五入之前尽可能准确。

我的里程计算器的代码如下:

<html>
    <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">

    var directionDisplay;
    var map;


function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    var copenhagen = new google.maps.LatLng(55.6771, 12.5704);
    var myOptions = {
        zoom:12,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: copenhagen
    }

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    directionsDisplay.setMap(map);
    }


    var directionsService = new google.maps.DirectionsService();

    function calcRoute() {
    var start = document.getElementById("start").value;
    var end = document.getElementById("end").value;
    var distanceInput = document.getElementById("distance");

    var request = {
        origin:start,
        destination:end,
        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 / 1609.3440006146921597227828997904;
        }
    });
    }
    </script>

    <title>Distance Calculator</title>

    <style type="text/css">

            body {
                font-family:Helvetica, Arial;
            }
            #map_canvas {
                height: 50%;
            }
    </style>
    </head>
    <body>

    <body onload="initialize()">
    <p>Enter your current location and desired destination to get the distance</p>
        <div>
            <p>
                <label for="start">Start: </label>
                <input type="text" name="start" id="start" />

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

                <input type="submit" value="Calculate Route" onclick="calcRoute()" />
            </p>
            <p>
                <label for="distance">Distance (miles): </label>
                <input type="text" name="distance" id="distance" readonly="true" />
            </p>

<p><a href="mailto:moniza@syedasadiq.co.uk?subject=Mileage&body="
   onclick="this.href += calcRoute('string', 'distance');"
>
Click to send email
</a></p>
        </div>
        <div id="map_canvas"></div>
    </body>
</html>

javascript google geolocation maps 
4

3 回答 3

3
var n = 1609.3440006146921597227828997904;

// Methods that JS provides
console.log(Math.round(n)); // 1609 (number)
console.log(n.toFixed(6)); // 1609.344001 (string)

// You can do this, if you prefer (but not recommended at all)
var round = function(n, d){
  return Math.round(n*Math.pow(10,d))/Math.pow(10,d);
};
console.log(round(n, 6)); // 1609.344001 (number)
于 2013-08-03T08:04:12.477 回答
1

你应该使用Number.toFixed,你传递一个参数来表示你希望在该点之后有多少位数:

var x = 23.142342341231;
console.log(x.toFixed(1));

获得固定数字后,您应该做的就是 parseFloat。

于 2013-08-03T08:02:27.603 回答
1

Math.round(num*10) 可以解决问题。但是您可能希望保留原始数字以进行进一步处理,以避免舍入精度错误。

这有帮助吗?

于 2013-08-03T08:13:07.747 回答