I've searched through google (maybe I didn't look hard enough) but I could not find how to turn Math.sqrt into an int.
I want to use Math.sqrt for a for loop and I guess I need it as an int but I can't seem to figure out how to cast the result to an int. So how do I do it?
I tried something similar to Java:
(int) Math.sqrt(num);
But it didn't work.
Thanks in advance :)
Return Values of Math. sqrt() method returns NaN i.e. Not a Number.
Math. sqrt() returns the square root of a value of type double passed to it as argument.
The Math.round() function returns the value of a number rounded to the nearest integer.
Use Math.round
, Math.ceil
, or Math.floor
depending on your specific rounding needs.
"For rounding numbers to integers one of Math.round, Math.ceil and Math.floor are preferable, and for a desired result that can be expressed as a 32 bit signed integer the bitwise operation described below might also suit."
-http://www.jibbering.com/faq/faq_notes/type_convert.html#tcNumber
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With