如何在javascript中取整
How to round in javascript
本文关键字:javascript 更新时间:2023-09-26
你好,我有里程计算器的代码,我希望显示的里程四舍五入到小数点后一位。这个的代码是什么?
不要只是说把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
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)
您应该使用Number.toFixed
,您可以传递一个参数来表示您希望在该点后面有多少位数:
var x = 23.142342341231;
console.log(x.toFixed(1));
在你得到固定的数字之后,你所要做的就是parseFloat。
Math.round(num*10)就可以了。但是,为了避免舍入精度误差,您可能需要保留原始数字以进行进一步处理。
这有帮助吗?
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 将函数的上下文应用于javascript变量
- 使用php或javascript从facebook相册URL中删除多余的部分
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 如何在JavaScript中将字符串转换为函数引用
- 模糊事件的Javascript测试
- Javascript更改图标
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 如何使用WCF服务和javascript表单post上传.doc文件
- javascript结合了数组和字典
- 这是什么 ==- javascript 运算符
- 从javascript创建一个列表
- 无法在通过jQuery的ajax加载的页面中执行javascript
- Javascript:selenium Web驱动程序isDisplayed()不工作
- 如何通过ajax刷新JSF填充的javascript变量
- 如何在Javascript中将JSon对象转换为数组
- Javascript生成的表单未提交
- 使用javascript将动态表从一个html页面打印到另一个html页
- 通过javascript重定向html传递php变量