4

如何找出十进制数的小数部分的长度或位数?

我可以看到一些方法,例如使用这样的字符串:

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

但是确定长度的最佳方法是什么?

如果我使用字符串,我可以想象出一些问题,例如,因为区域设置和数字格式可能因系统而异。有没有很好的计算方法?也许没有迭代?

提前致谢。

4

6 回答 6

0

看起来很多人提供了大十进制。至少在眼睛上很容易。该代码应为您工作。

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));
    }
}
于 2011-03-15T17:24:52.110 回答
0

您可以使用 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 ())          

您可以根据需要设置 Decimal-Identifier,并使用正则表达式切断前导部分,并使用 String.length() 计算其余部分。

于 2011-03-15T17:10:13.147 回答
0

尝试这个:

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();
} 

编辑:

如果数字实际上是一个整数(即 0 的小数),它仍然会返回 1。因此,您可以先检查是否真的有小数部分。

编辑2:

stripTrailingZeros()似乎可以解决问题,除了 0.0d。相应地更新了代码。

于 2011-03-15T16:53:00.960 回答
0

我只是为此写了一个简单的方法,希望它可以帮助某人。

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;
}
于 2015-10-10T19:42:45.363 回答
0

那是我最好的实现:

public class NumberHandler {

    private static NumberFormat formatter = NumberFormat.getInstance(Locale.ENGLISH);

    static {
        formatter.setMaximumFractionDigits(Integer.MAX_VALUE);
        formatter.setGroupingUsed(false);
    }

    public static int getFractionLength(double doubleNumber) {
        String numberStr = formatter.format(doubleNumber);
        int dotIndex = numberStr.indexOf(".");
        if (dotIndex < 0) {
            return 0;
        } else {
            return numberStr.length() - (dotIndex + 1);
        }
    }

}

效果不佳,可能并不完美,但其他选择更糟。

于 2018-06-06T06:19:06.590 回答
-1
public static int getNumberOfFractionDigits(double d) {
    String s = Double.toString(d), afterDecimal="";
    afterDecimal = s.subString(s.indexOf(".") + 1, s.length() - 1);
    return afterDecimal.length();
}
于 2011-03-15T17:01:56.897 回答