Javascript 四舍五入数字-数学
我在谷歌地图应用程序中有以下内容,希望显示转换为英尺的高程,但如何向上/向下取整到最接近的数字?(消除小数点后的数字)我尝试了Javascript 四舍五入数字-数学,javascript,math,google-maps,Javascript,Math,Google Maps,我在谷歌地图应用程序中有以下内容,希望显示转换为英尺的高程,但如何向上/向下取整到最接近的数字?(消除小数点后的数字)我尝试了number.toFixed(x)方法,但似乎没有任何效果 function getElevation(event) { var locations = []; var clickedLocation = event.latLng; locations.push(clickedLocation); var positionalReques
number.toFixed(x)
方法,但似乎没有任何效果
function getElevation(event) {
var locations = [];
var clickedLocation = event.latLng;
locations.push(clickedLocation);
var positionalRequest = { 'locations': locations }
// Initiate the location request
elevator.getElevationForLocations(positionalRequest, function(results, status) {
if (status == google.maps.ElevationStatus.OK) {
// Retrieve the first result
if (results[0]) {
// Open an info window indicating the elevation at the clicked position
infowindow.setContent("The elevation at this point <br/>is " + results[0].elevation*(3.2808399) + " feet.");
infowindow.setPosition(clickedLocation);
infowindow.open(map);
} else {
alert("No results found");
}
} else {
alert("Elevation service failed due to: " + status);
}
});
}
函数getElevation(事件){
var位置=[];
var clickedLocation=event.latLng;
位置。推送(单击位置);
变量positionRequest={'locations':locations}
//启动位置请求
电梯。getElevationForLocations(位置请求、功能(结果、状态){
if(status==google.maps.ElevationStatus.OK){
//检索第一个结果
如果(结果[0]){
//打开一个信息窗口,指示单击位置的高程
infowindow.setContent(“此时的高程
为“+results[0]。高程*(3.2808399)+“英尺”);
信息窗口。设置位置(单击位置);
打开(地图);
}否则{
警报(“未发现结果”);
}
}否则{
警报(“由于:+状态,提升服务失败);
}
});
}
如果您想四舍五入到最接近的整数,只需使用Math.round(x)
您可能还需要查看Math.floor
(始终向下舍入)和Math.ceil
(始终向上舍入)
我制作了一把小提琴,演示了这三种方法:Math.floor(n)
返回向下舍入到最接近整数的数字
Math.ceil(n)
返回向上舍入到最接近整数的数字
Math.round(n)
返回四舍五入到最接近的整数的数字谢谢。结果是
Math.round(结果[0].elevation*(3.2808399))+“feet.”
Math.round(25.9) //returns 26
Math.round(25.2) //returns 25
Math.round(-2.58) //returns -3