Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript:获取整数的90%,精确到最接近的整数?_Javascript - Fatal编程技术网

JavaScript:获取整数的90%,精确到最接近的整数?

JavaScript:获取整数的90%,精确到最接近的整数?,javascript,Javascript,我正在用JavaScript开发谷歌地图标记。其高度被创建为google.maps.Point,必须具有整数宽度和高度 这是我的问题。我希望标记阴影的宽度是标记高度的90%。我事先不知道高度 如果我设置: var shadowDimensions = new google.maps.Point(height*0.9,height); 而且height*0.9不是整数,我得到一个错误 如何在JavaScript中定义“height*0.9到最接近的整数” 谢谢 取决于最近距离的含义,但通常需要使

我正在用JavaScript开发谷歌地图标记。其高度被创建为google.maps.Point,必须具有整数宽度和高度

这是我的问题。我希望标记阴影的宽度是标记高度的90%。我事先不知道高度

如果我设置:

var shadowDimensions = new google.maps.Point(height*0.9,height);
而且
height*0.9
不是整数,我得到一个错误

如何在JavaScript中定义“
height*0.9
到最接近的整数”


谢谢

取决于最近距离的含义,但通常需要使用
Math.round()
。或者,您可以使用
Math.floor()
(向下舍入)或
Math.ceil()
(向上舍入)


另请参见。

使用方法的规范

我知道Javascript有一个Round函数,我想不起它的语法

可能是这样的:

var shadowDimensions = new google.maps.Point(Math.Round(height*0.9),height);
或者是天花板/地板功能


希望这对您有所帮助

您可能希望尝试
parseInt(height*0.9)
使用,它四舍五入到最接近的整数,四舍五入为.5:

var shadowDimensions = new google.maps.Point(Math.round(height*0.9),height);

四舍五入到下一个较小的整数(对于负数,远离零)是使用完成的,而到下一个较大整数(对于负数,接近零)的四舍五入是使用完成的。另请参见。

这与
数学地板
相同,这可能是意外的。
var shadowDimensions = new google.maps.Point(Math.round(height*0.9),height);