2017-02-08 10 views
0

Ich möchte ein BigDecimal Wert formatiert werden nach folgenden Regeln:Format BigDecimal in Java

25 => 25 
25,1 => 25,10 
25,10 => 25,10 
25,12 = > 25,12 

Ich habe die Foren gesucht, aber keine passende Frage gefunden (here, here und here) und ich ausgesehen haben bei der Javadoc für BigDecimal und NumberFormat ohne zu verstehen, wie dies zu tun ist.

EDIT: Heute tue ich:

NumberFormat currencyFormat02 = NumberFormat.getCurrencyInstance(locale); 
currencyFormat02.setMinimumFractionDigits(0); 
currencyFormat02.setMaximumFractionDigits(2); 
currencyFormat02.setGroupingUsed(false); 
BigDecimal bd = new BigDecimal("25 or 25,1 or 25,10 or 25,12"); 
String x =currencyFormat02.format(bd); 

x wie oben gedruckt werden soll tut aber nicht.

+1

Beispiele sind groß, aber Sie müssen auch Geben Sie an, was Sie zu tun versuchen. – shmosel

+0

Ich habe die Frage aktualisiert – user1329339

+0

Sie haben Ihr Ziel immer noch nicht beschrieben. – shmosel

Antwort

1

Sie müssen dann zwischen BigDecimal Werten unterscheiden, die „ganze Zahlen“ darstellen; und diejenigen, die das nicht tun.

Etwas wie:

BigDecimal someNumber = ... 
if (someNumber.toBigIntegerExact()) { 
// go for the 25 kind of formatting 
} else { 
// go for the 25.xx kind of formatting 

Die "25.xx" Formatierung wird beschrieben schön here (oder in der anderen Antwort von Leonardo)

0

Um BigDecimal-Format mit Numberformat, müssen Sie Locale zuerst einstellen versuchen, so etwas wie dieses:

String strToFormat = "25.10"; 
Locale loc = new Locale("en","US"); 

DecimalFormat numFormat = (DecimalFormat)NumberFormat.getInstance(loc); 
numFormat.setParseBigDecimal(true); 
BigDecimal yourValue = (BigDecimal)numFormat.parse(strToFormat, new ParsePosition(0)); 

System.out.println("Value : " + yourValue); 

Ergebnis:

Value : 25.10 
+1

Das wird 25 zu "25,00" formatieren – GhostCat

+1

Ja, das ist nicht korrekt. – user1329339

+0

Test bearbeitet und Ergebnis nach dem Ausführen des Codes hinzugefügt –

1

Wahrscheinlich nicht die effizienteste, aber man könnte so etwas wie dies versuchen:

import java.math.BigDecimal; 
import java.text.DecimalFormat; 
import java.text.DecimalFormatSymbols; 
import java.util.Locale; 

class Main { 
    public static void main(String[] args) { 
    BigDecimal ex1 = new BigDecimal("25"); 
    BigDecimal ex2 = new BigDecimal("25.1"); 
    BigDecimal ex3 = new BigDecimal("25.10"); 
    BigDecimal ex4 = new BigDecimal("25.12"); 
    printCustomBigDecimalFormat(ex1); 
    printCustomBigDecimalFormat(ex2); 
    printCustomBigDecimalFormat(ex3); 
    printCustomBigDecimalFormat(ex4); 
    } 

    public static void printCustomBigDecimalFormat(BigDecimal bd) { 
    DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.getDefault()); 
    symbols.setDecimalSeparator(','); 
    DecimalFormat df = new DecimalFormat("##.00", symbols); 
    if(containsDecimalPoint(bd)) { 
     System.out.println(df.format(bd)); 
    } else { 
     System.out.println(bd); 
    } 
    } 

    private static boolean containsDecimalPoint(BigDecimal bd) { 
    return bd.toString().contains("."); 
    } 
} 

Ausgang:

25 
25,10 
25,10 
25,12 

Probieren Sie es here!

+1

Ich mag den [mcve] Aspekt in Ihrem Beispiel! – GhostCat