Calculating powers of integers

前端 未结 16 2016
情书的邮戳
情书的邮戳 2020-12-08 06:18

Is there any other way in Java to calculate a power of an integer?

I use Math.pow(a, b) now, but it returns a double, and that is usually a

16条回答
  •  春和景丽
    2020-12-08 06:37

    Well you can simply use Math.pow(a,b) as you have used earlier and just convert its value by using (int) before it. Below could be used as an example to it.

    int x = (int) Math.pow(a,b);
    

    where a and b could be double or int values as you want. This will simply convert its output to an integer value as you required.

提交回复
热议问题