How to restrict the value of the variable to only two decimal places?

Asked

Viewed 1,714 times

1

I want the 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))))));

1 answer

4


You can use these two methods that leave the truncated values

If you want the result to be 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 passing as attribute the value of r, that it will be converted to a value with only two houses after the comma

  • Thank you! All right!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.