How to restrict the value of the variable with only two decimal places? [closed]

1

I want r with only two decimal places.

double r = Math.abs ((aux5 - ((aux1*aux2)/previsoes.length))/ (Math.sqrt(Math.abs((aux3 - ((aux1 * aux1) / previsoes.length)) * (aux4 - ((aux2 * aux2) / previsoes.length))))));
    
asked by anonymous 29.11.2017 / 20:07

1 answer

4

You can use these two methods that leave the truncated values

If you want the results are a double:

public static double truncate(double value) {
    return Math.round(value * 100) / 100d;
}

If you want a String:

public static String truncate(double value) {
    DecimalFormat df = new DecimalFormat("#.00");
    return df.format(value);
}

Now just call the method by passing the value of r as an attribute, it will be transformed to a value with only two houses after the comma

    
29.11.2017 / 20:18