Java BigInteger Value Check isNumberInRange(String text, BigInteger min, BigInteger max)

Here you can find the source of isNumberInRange(String text, BigInteger min, BigInteger max)

Description

Check whether the number is in range [min, max] inclusive.

License

Apache License

Parameter

Parameter Description
text a parameter
min a parameter
max a parameter

Declaration

public static boolean isNumberInRange(String text, BigInteger min, BigInteger max) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.math.BigInteger;

public class Main {
    /**/*from  w ww .j  a  va2 s . co m*/
     * Check whether the number is in range [min, max] inclusive.
     * <p> Any/Both of the min/max could be null, which would be ignored.
     * <p> Note, it's your responsibility to ensure min <= max if neither of them is null.
     * @param text
     * @param min
     * @param max
     * @return
     */
    public static boolean isNumberInRange(String text, BigInteger min, BigInteger max) {
        BigInteger value = null;
        try {
            value = new BigInteger(text);
        } catch (Exception e) {
            // could be NullPointerException and MalformatException.
            return false;
        }

        if (min != null) {
            if (value.compareTo(min) < 0) {
                // smaller then min.
                return false;
            }
        }

        if (max != null) {
            if (value.compareTo(max) > 0) {
                return false;
            }
        }

        return true;
    }
}

Related

  1. isIntValue(BigInteger bi)
  2. isLessThan(BigInteger valueA, BigInteger valueB)
  3. isLong(BigInteger number)
  4. isMersenneNumber(BigInteger n)
  5. isNegative(BigInteger i)
  6. isOdd(BigInteger in)
  7. isOdd(BigInteger x)
  8. isPerfectCubic(BigInteger n)
  9. isPositive(final BigInteger value)