Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check number of digits from BigDecimal?

Tags:

java

The requirement is to check if the number of digits is less than 7 digits in that case insert in DB else don't. I have tried the following solutions:

First solution:

public static void checkNoOfDigitVal(BigDecimal bigDecVal) {
    BigInteger digits = bigDecVal.toBigInteger();
    BigInteger ten = BigInteger.valueOf(10);
    int count = 0;
    do {
        digits = digits.divide(ten);
        count++;
    } while (!digits.equals(BigInteger.ZERO));
    System.out.println("Number of digits : " + count);
}

First solution works fine sometimes but sometimes the condition in while loop is not satisfied and it keeps on increasing the count number leading to endless count.

Second solution:

public static void checkNoOfDigitsVal(BigDecimal bigDecVal) {
    String string = bigDecVal.toString();
    String[] splitedString = string.split("\\.");
    String[] newVal = splitedString[0].split("");
    int a = newVal.length - 1;
    if (a <= 6) {
        System.out.println("correct size insert into DB: " + a);
    } else {
        System.out.println("Incorrect size insert cancel: " + a);
    }
}

For example, if the value is 999999.9999, the second solution will return newVal.length = 6.

Please suggest a better solution to check the number of digits for big decimal where looping overhead can be minimized.

like image 250
Harleen Avatar asked Mar 04 '16 09:03

Harleen


People also ask

How do I find the number of decimal places in BigDecimal?

math. BigDecimal. scale() is an inbuilt method in java that returns the scale of this BigDecimal. For zero or positive value, the scale is the number of digits to the right of the decimal point.

How do you find the whole number in BigDecimal?

intValue() is an in-built function which converts this BigDecimal to an integer value. This function discards any fractional part of this BigDecimal. If the result of the conversion is too big to be represented as an integer value, the function returns only the lower-order 32 bits.

Does BigDecimal have decimal places?

Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point.


1 Answers

You can get it trivially using:

static int integerDigits(BigDecimal n) {
    n = n.stripTrailingZeros();
    return n.precision() - n.scale();
}

The precision is the total number of digits, and the scale is how many of those are to the right of the decimal point, so the difference is how many are to the left of the decimal point.

EDIT it's necessary to remove any trailing zeros to get a correct result for e.g. 0.000

EDIT 2 alternatively (and acks to @AdrianShum), since the problem with trailing zeroes only manifests itself with 0.00... you could use:

static int integerDigits(BigDecimal n) {
    return n.signum() == 0 ? 1 : n.precision() - n.scale();
}

Live demo at http://ideone.com/uI6iMG

like image 197
Alnitak Avatar answered Oct 21 '22 14:10

Alnitak