2016-07-13 24 views

Antwort

3

Sie können nur die setMaximumFractionDigits auf 1 gesetzt wie folgt aus:...

public class Test { 

public static void main(String[] args) { 
    System.out.println(format(14.0184849945)); // prints '14.0' 
    System.out.println(format(13)); // prints '13' 
    System.out.println(format(3.5)); // prints '3.5' 
    System.out.println(format(3.138136)); // prints '3.1' 
} 

public static String format(Number n) { 
    NumberFormat format = DecimalFormat.getInstance(); 
    format.setRoundingMode(RoundingMode.FLOOR); 
    format.setMinimumFractionDigits(0); 
    format.setMaximumFractionDigits(1); 
    return format.format(n); 
} 

} 

Dies kann hilft Ihnen

0

Versuchen Sie folgendes:

public static String customFormat(String pattern, double value) { 
    NumberFormat nf = NumberFormat.getNumberInstance(Locale.ENGLISH); 
    DecimalFormat df = (DecimalFormat)nf; 
    df.applyPattern(pattern); 
    return df.format(value); 

} 

Berufung:

double x = Double.parseDouble(customFormat("###.#", a/b); 

Diese Methode auch die Zahlen nicht aufrunden.

Für die englische locale:

, ist nicht das Dezimalsystem Splitter. Dies gilt. ##, ##, ##, ## ###

. das Dezimalsystem Teiler ist. Dies gilt nicht: ## ## ## ##, ### (zumindest nicht für diese locale)