Frage

Ich habe ein DecimalFormat Objekt, das ich verwende alle meine Doppelwerte auf eine festgelegte Anzahl von Ziffern zu formatieren (sagen wir mal 2), wenn ich sie habe die Anzeige. Ich würde es normalerweise gerne formatiert werden auf 2 Dezimalstellen, aber ich will immer mindestens eine signifikante Stelle. Zum Beispiel ist, wenn mein Wert 0,2 dann meine Formatierer spucken 0,20 und das ist großartig. Wenn jedoch mein Wert 0.000034 meine Formatter ist, werden 0,00 ausspucken und ich würde mein Formatter ausspucken 0,00003 bevorzugen.

Die Zahl Formatierer in Objective-C dies tut sehr einfach, kann ich nur eine maximale Anzahl von Ziffern I bei 1 zu 2 und die minimalen Anzahl von signifikanten Stellen angezeigt werden soll und es produziert meine gewünschte Ausgabe, aber wie kann ich tut es in Java?

Ich schätze jede Hilfe mir jemand anbieten können.

Kyle

Edit:. Ich habe Interesse, die Werte so 0.000037 Displays als 0,00004 Rundungs ??

War es hilfreich?

Lösung

Es ist nicht effizient, so dass, wenn Sie diesen Vorgang ausführen oft würde ich eine andere Lösung versuchen, aber wenn man es nur nennen gelegentlich diese Methode funktioniert.

import java.text.DecimalFormat;
public class Rounder {
    public static void main(String[] args) {
        double value = 0.0000037d;
        // size to the maximum number of digits you'd like to show
        // used to avoid representing the number using scientific notation
        // when converting to string
        DecimalFormat maxDigitsFormatter = new DecimalFormat("#.###################");
        StringBuilder pattern = new StringBuilder().append("0.00");
        if(value < 0.01d){
            String s = maxDigitsFormatter.format(value);
            int i = s.indexOf(".") + 3;
            while(i < s.length()-1){
                pattern.append("0");
                i++;
            }
        }
        DecimalFormat df = new DecimalFormat(pattern.toString());
        System.out.println("value           = " + value);
        System.out.println("formatted value = " + maxDigitsFormatter.format(value));
        System.out.println("pattern         = " + pattern);
        System.out.println("rounded         = " + df.format(value));
    }
}

Andere Tipps

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


public class Test {

    public static void main(String[] args) {
        String input = 0.000034+"";
        //String input = 0.20+"";
        int max = 2;
        int min =1;
        System.out.println(getRes(input,max,min));
    }

    private static String getRes(String input,int max,int min) {
        double x = Double.parseDouble(((new BigDecimal(input)).unscaledValue().intValue()+"").substring(0,min));
        int n = (new BigDecimal(input)).scale();
        String res = new BigDecimal(x/Math.pow(10,n)).round(MathContext.DECIMAL64).setScale(n).toString();
        if(n<max){
            for(int i=0;i<max;i++){
                res+="0";
            }
        }
        return res;
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top