codewars-016: Overthrowing with longing

 https://www.codewars.com/kata/551af235ac044763f2000448/train/javajava

// https://www.codewars.com/kata/551af235ac044763f2000448/train/java
package codewars;
import java.math.BigInteger;
public class Multiplier{
  public static long multiply(long a, long b){
    BigInteger m = new BigInteger(String.valueOf(a));
    BigInteger n = new BigInteger(String.valueOf(b));
    BigInteger rslt = m.multiply(n);
    if(rslt.compareTo(BigInteger.ZERO) > 0 && rslt.compareTo(new BigInteger(String.valueOf(Long.MAX_VALUE))) > 0 || rslt.compareTo(BigInteger.ZERO) < 0 && rslt.compareTo(new BigInteger(String.valueOf(Long.MIN_VALUE))) < 0 ){
      throw new ArithmeticException();
    }
    return Long.valueOf(rslt.toString());
  }
}
相關文章
相關標籤/搜索