4

10 進数の小数部分の長さや桁数を調べるにはどうすればよいですか?

たとえば、次のような文字列を使用した場合、いくつかのアプローチを見ることができます。

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

しかし、長さを決定する最良の方法は何ですか?

文字列を使用すると、システムごとにロケールと数値の形式が異なる可能性があるため、いくつかの問題が発生する可能性があります。うまく計算する方法はありますか?多分反復なし?

前もって感謝します。

4

6 に答える 6

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

多くの人が Big Decimal を提供したようです。少なくとも目にはやさしい。コードはあなたのために働くでしょう。

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

これを試して:

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

編集:

数値が実際に iter (すなわち 0 の小数) である場合でも、これは 1 を返します。したがって、最初に実際に小数部分があるかどうかを確認することができます。

編集2:

stripTrailingZeros()0.0dを除いて、トリックを行うようです。それに応じてコードを更新しました。

于 2011-03-15T16:53:00.960 に答える
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 に答える
-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 に答える