How to check number of digits from BigDecimal?
<p>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:</p>

<p>First solution:</p>

<pre><code>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);
}
</code></pre>

<p>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.</p>

<p>Second solution:</p>

<pre><code>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 &lt;= 6) {
        System.out.println("correct size insert into DB: " + a);
    } else {
        System.out.println("Incorrect size insert cancel: " + a);
    }
}
</code></pre>

<p>For example, if the value is <code>999999.9999</code>, the second solution will return <code>newVal.length = 6</code>.</p>

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