Skip to content

Instantly share code, notes, and snippets.

@hilfritz
Created July 17, 2018 06:44
Show Gist options
  • Select an option

  • Save hilfritz/3164c272f27d4f73dce4f4b8a413aa1b to your computer and use it in GitHub Desktop.

Select an option

Save hilfritz/3164c272f27d4f73dce4f4b8a413aa1b to your computer and use it in GitHub Desktop.
java decimal places for double
/**
* from here: https://stackoverflow.com/questions/22186778/using-math-round-to-round-to-one-decimal-place
* @param value double value with decimal places 123.123456789
* @param precision 1 number of decimal places desired ex. 1 (result => 123.1)
* @return
*/
public static double roundToDecimalPlaces(double value, int precision) {
int scale = (int) Math.pow(10, precision);
return (double) Math.round(value * scale) / scale;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment