Java - Double

> Procedural Languages > Java

1 - About

3 - Snippet

3.1 - Round

public static double round(double value, int places) {
        if (places < 0) throw new IllegalArgumentException();
 
        BigDecimal bd = new BigDecimal(value);
        bd = bd.setScale(places, RoundingMode.HALF_UP);
        return bd.doubleValue();
    }

3.2 - Setting the radix (decimal separator)

Double.parseDouble always uses a dot as the decimal separator and does not use the default locale. To interpret localized string representations of a floating-point value, use subclasses of NumberFormat.

Example, setting the radix to a , comma:

DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance();
DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
// Set decimal separator
decimalFormatSymbols.setDecimalSeparator(',');
decimalFormat.setDecimalFormatSymbols(decimalFormatSymbols);
String val = "10,2";
Double doubleValue = decimalFormat.parse(val).doubleValue();
Advertising

3.3 - To Integer

Double myDouble = 3.41;
myDouble.intValue()
lang/java/double.txt · Last modified: 2019/08/19 12:06 by gerardnico