How to get the length of a fractional part?

How to find out the length or number of digits of the fractional part of a decimal number?

I see several approbations, for example. with the following lines:

public static int getNumberOfFractionDigits(Number number) {
    Double fractionPart = number.doubleValue() - number.longValue();
    return fractionPart.toString().length() - 2;
}

But what is the best way to determine the length?

I could introduce some problems if I use strings, for example. because the format of the language and number may differ from system to system. Is there a good way to calculate it? Maybe without iterations?

Thanks in advance.

+3
source share
5 answers

I just wrote a simple method for this, hope it can help someone.

public static int getFractionDigitsCount(double d) {
    if (d >= 1) { //we only need the fraction digits
        d = d - (long) d;
    }
    if (d == 0) { //nothing to count
        return 0;
    }
    d *= 10; //shifts 1 digit to left
    int count = 1;
    while (d - (long) d != 0) { //keeps shifting until there are no more fractions
        d *= 10;
        count++;
    }
    return count;
}
+1
source

Try the following:

public static int getNumberOfFractionDigits(Number number) {
 if( number == null ) return 0; //or throw
 if( number.doubleValue() == 0.0d ) return 0;
 BigDecimal bd = new BigDecimal(number.toString()); 
 //BigDecimal bd = BigDecimal.valueOf(number.doubleValue()); // if double precision is ok, just note that you should use BigDecimal.valueOf(double) rather than new BigDecimal(double) due to precision bugs in the latter
 bd = bd.stripTrailingZeros(); //convert 1.00 to 1 -> scale will now be 0, except for 0.0 where this doesn't work
 return bd.scale();
} 

Edit:

iteger (.. 0), 1. , , .

Edit2:

stripTrailingZeros() , , 0,0d. .

0

You can use java.text.NumberFormat.

nf = java.text.NumberFormat.getInstance (); 
// without BigDecimal, you will reach the limit far before 100  
nf.setMaximumFractionDigits (100);
String s = nf.format (number.doubleValue ())          

You can set the decimal identifier as you wish and use regular expressions to clip the main part and String.length () to evaluate the rest.

0
source

Many seem to have suggested Big Decimal. At least in the eyes. The code will work for ya.

package t1;

import java.math.*;

public class ScaleZ {
    private static final int MAX_PRECISION = 10;
    private static final MathContext mc = new MathContext(MAX_PRECISION, RoundingMode.HALF_EVEN);
    public static int getScale(double v){
        if (v!=v || v == Double.POSITIVE_INFINITY || v == Double.NEGATIVE_INFINITY)
            return 0;//throw exception or return any other stuff

        BigDecimal d = new BigDecimal(v, mc);
        return Math.max(0, d.stripTrailingZeros().scale());
    }


    public static void main(String[] args) {
        test(0.0);
        test(1000d);
        test(1d/3);
        test(Math.PI);
        test(1.244e7);
        test(1e11);
    }

    private static void test(double d) {
        System.out.printf("%20s digits %d%n", d, getScale(d));
    }
}
0
source
public static int getNumberOfFractionDigits(double d) {
    String s = Double.toString(d), afterDecimal="";
    afterDecimal = s.subString(s.indexOf(".") + 1, s.length() - 1);
    return afterDecimal.length();
}
-1
source

All Articles