Javascript:将Math.sqrt转换为int?

Nop*_*ope 12 javascript math int casting type-conversion

我通过谷歌搜索(也许我看起来不够努力),但我找不到如何将Math.sqrt变成int.

我想使用Math.sqrt作为for循环,我想我需要它作为一个int,但我似乎无法弄清楚如何将结果转换为int.那我该怎么做?

我尝试过类似于Java的东西:

(int) Math.sqrt(num);
Run Code Online (Sandbox Code Playgroud)

但它没有用.

提前致谢 :)

Chr*_*nce 24

使用Math.round,, Math.ceilMath.floor根据您的特定舍入需求.

"对于将数字四舍五入为整数,Math.round,Math.ceil和Math.floor之一是优选的,并且对于可以表示为32位有符号整数的期望结果,下面描述的按位运算也可能适用."

- http://www.jibbering.com/faq/faq_notes/type_convert.html#tcNumber


Nos*_*dna 5

有人建议使用parseInt.从字符串到int,但很容易将float转换为字符串.

parseInt(Math.sqrt(num)+"")
Run Code Online (Sandbox Code Playgroud)

请记住,无论你做什么,JavaScript总是使用浮动.没有整数类型.