Hallo,
ich habe ein Problem mit Java 8. Ich hatte davor eine Methode, die mir eine Zahl entsprechend formatiert. In Java7 hatte das m.W. auch funktioniert.
ich habe dazu eine Klasse, in der ich einstelle wieviel Dezimalstellen meine Zahl haben soll etc. (Klasse: FormatSetting).
Mein Problem ist nun aber, dass ich im Parameter mit 10 reingehe, ich aber nicht 10,00 bekomme, sondern 0,10.
Kann mir jemand helfen wo das Problem ist?
Ich vermute hier:
Aber auch hier bekomme ich #0,00 war doch eigentlich richtig ist?
Hier mal die Ausgabe im Debugger von DecimalFormat dc

Hier meine Werte für die Einstellung:

ich habe ein Problem mit Java 8. Ich hatte davor eine Methode, die mir eine Zahl entsprechend formatiert. In Java7 hatte das m.W. auch funktioniert.
ich habe dazu eine Klasse, in der ich einstelle wieviel Dezimalstellen meine Zahl haben soll etc. (Klasse: FormatSetting).
Mein Problem ist nun aber, dass ich im Parameter mit 10 reingehe, ich aber nicht 10,00 bekomme, sondern 0,10.
Kann mir jemand helfen wo das Problem ist?
Ich vermute hier:
Java:
String format = "#0" + String.format("%." + formatSettings.getNumberDecimalPlaces() + "f", .1)
.replace("1", "0").substring(1);
Aber auch hier bekomme ich #0,00 war doch eigentlich richtig ist?
Java:
public static String formatNumber(FormatSetting formatSettings, Number d) {
StringBuilder value = new StringBuilder();
try {
if (formatSettings != null) {
DecimalFormatSymbols dcm = new DecimalFormatSymbols(Locale.getDefault());
if (formatSettings.getNumberDecimalSeparator() != null)
dcm.setDecimalSeparator(formatSettings.getNumberDecimalSeparator().charAt(0));
if (formatSettings.getNumberThousandSeparator() != null)
dcm.setGroupingSeparator(formatSettings.getNumberThousandSeparator().charAt(0));
String format = "#0" + String.format("%." + formatSettings.getNumberDecimalPlaces() + "f", .1)
.replace("1", "0").substring(1);
if (formatSettings.getNumberPattern() != null
&& !FormatSetting.Pattern.NONE.toString().equals(formatSettings.getNumberPattern())) {
format = "+" + formatSettings.getNumberPattern() + ";-" + formatSettings.getNumberPattern();
}
DecimalFormat dc = new DecimalFormat(format, dcm);
String numberStr = dc.format(d);
if (formatSettings.getNumberNegativeSignSymbol() != null && FormatSetting.SignPosition.BEGINNING
.toString().equals(formatSettings.getNumberNegativeSignPosition())) {
if (d.doubleValue() < 0)
value.append(formatSettings.getNumberNegativeSignSymbol());
}
value.append(numberStr);
if (formatSettings.getNumberNegativeSignSymbol() != null && FormatSetting.SignPosition.END.toString()
.equals(formatSettings.getNumberNegativeSignPosition())) {
if (d.doubleValue() < 0)
value.append(formatSettings.getNumberNegativeSignSymbol());
}
}
return value.toString();
} catch (Exception ex) {
ex.printStackTrace();
value = new StringBuilder("" + d);
}
return "";
}
Hier mal die Ausgabe im Debugger von DecimalFormat dc

Hier meine Werte für die Einstellung:
