0

値を特定の小数点以下の桁数に丸めるのは簡単です。

public static double round(double x, int n){
    n = (int)Math.pow(n, 10);
    return Math.round(x*n)/n;
}

しかし、数値の大きさに応じて実際に切り上げる必要がある小数点以下の桁数を想定できるアルゴリズムを作成できますか?

つまり、数値が大きい場合(のように100000000.12345)、小数点以下の精度はそれほど必要ないので、小数点以下1桁または2桁に切り上げることができますが 、

数値が非常に小さい場合(のように)、0.00012345必要になります。最大小数精度
このようなことをするためのアイデアは何ですか?

編集: 例:

 argument       returned   decnum           comment
123456789.9    123456789     0    //number is way too large so I don't need any decimal places
12345678.99    12345678      0    //number is still very large.
1234567.899    1234567       0    //still...
123456.7899    123456        0    //...
12345.67899    12345         0    //...
1234.567899    1234          0    //It's pretty large now. Still not small enough to get some decimals.
123.4567899    123.4         1    //Now, number is a little average, so 1 decimal place is included.
12.34567899    12.34         2    //The number is normal, so I want a normal 2 decimal places 
1.234567899    1.234         3    //Now it's kinda small, so it now gives 3 decimal places
.1234567899    0.1234        4    //Smaller number, thus greater number of decimal places (4).
.0123456789    0.01234       5    //Even smaller
.0012345678    0.0012345     7    //Larger number of decimal places.
.0001234567    0.0001234567  10   //Smaller and smaller number, greater and greater number of decimals.

数がゼロに近づいたときに小数の数を増やし 、数がゼロから離れるにつれて小数を減らす
ことができる方法を探しています。

4

3 に答える 3

3

を使用できますBigDecimalBigDecimal値を使用してを作成し、doubleそれによって返される値とその値に応じて、必要なスケールでスケールを確認しbigDVariable.scale()、丸めます。BigDecimalドキュメント。

編集:そもそも小数点以下の桁数について心配する必要はないようですdouble。重要な情報は、自分の値だけであるように見えます。その場合、チェックscale()は重要ではありません。

于 2013-03-12T15:43:50.740 に答える
1

アルゴリズムの観点から、私は以下の手順に従います。

  1. 数値を基数10の形式に変換します(例:123456789.9-> 1.234567899 * 10^8および0.0001234567->1.234567* 10 ^-4)。

  2. 8たとえば最初の例と-4最後の例で、力率10を見てください。

  3. 係数を調整して、精度の高い場所の要件を計算します。ほんの一例は、のようになります。(-1*(p-3))ここpで、は力率です。数値が負になる場合は、「0」を使用します。

これにより、次の数値が得られます。

argument       decnum      
123456789.9    -1*(8-3) = -5 -> 0
12345678.99    -1*(7-3) = -4 -> 0
1234567.899    -1*(6-3) = -3 -> 0
123456.7899    -1*(5-3) = -2 -> 0
12345.67899    -1*(4-3) = -1 -> 0
1234.567899    -1*(3-3) =  0 -> 0
123.4567899    -1*(2-3) = 1  -> 1
12.34567899    -1*(1-3) = 2  -> 2
1.234567899    -1*(0-3) = 3  -> 3
.1234567899    -1*(-1-3) = 4 -> 4
.0123456789    -1*(-2-3) = 5  -> 5
.0012345678    -1*(-3-3) = 6  -> 6
.0001234567    -1*(-4-3) = 7  -> 7

これはあなたが探しているものに非常に近いでしょう。本当に必要な場合は、係数と式を調整して、より近づけてみてください(これは単なるアルゴリズム/アプローチです)。

于 2013-03-12T16:18:13.423 に答える
1

高精度(有効数字)が必要な場合はBigDecimal、doubleの代わりに使用する必要があります。ラウンド方式では、BigDecimal必要な有効桁数が得られます。

これが私がまとめたものです。

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

public class Rounding {

    public static void main(String[] args) {
        BigDecimal number = new BigDecimal(100000000.12345);
        System.out.println("Number: " + number.toPlainString());
        System.out.println(" ");
        for (int precision = 8; precision < 17; precision++) {
            System.out.println("Precision: " + precision + ", result: "
                    + round(number, precision));
        }
    }

    public static String round(BigDecimal number, int precision) {
        MathContext mathContext = new MathContext(precision, 
                RoundingMode.HALF_UP);
        BigDecimal rounded = number.round(mathContext);
        return rounded.toPlainString();
    }

}

そして、これが結果です。

Number: 100000000.12344999611377716064453125

Precision: 8, result: 100000000
Precision: 9, result: 100000000
Precision: 10, result: 100000000.1
Precision: 11, result: 100000000.12
Precision: 12, result: 100000000.123
Precision: 13, result: 100000000.1234
Precision: 14, result: 100000000.12345
Precision: 15, result: 100000000.123450
Precision: 16, result: 100000000.1234500

Number: 0.00012344999999999999203137424075293893110938370227813720703125

Precision: 8, result: 0.00012345000
Precision: 9, result: 0.000123450000
Precision: 10, result: 0.0001234500000
Precision: 11, result: 0.00012345000000
Precision: 12, result: 0.000123450000000
Precision: 13, result: 0.0001234500000000
Precision: 14, result: 0.00012345000000000
Precision: 15, result: 0.000123450000000000
Precision: 16, result: 0.0001234500000000000
于 2013-03-12T16:48:03.740 に答える