Skip to main content
  1. About
  2. For Teams
Without round brackets does not work as expected
Source Link

A succinct solution:

   public static double round(double value, int precision) {
      int scale = (int) Math.pow(10, precision);
      return (double) (Math.round(value * scale) / scale;scale);
  }

See also, https://stackoverflow.com/a/22186845/212950 Thanks to jpdymond for offering this.

Edit: Added round brackets. Casts the whole result to double, not the first argument only!

A succinct solution:

   public static double round(double value, int precision) {
      int scale = (int) Math.pow(10, precision);
      return (double) Math.round(value * scale) / scale;
  }

See also, https://stackoverflow.com/a/22186845/212950 Thanks to jpdymond for offering this.

A succinct solution:

   public static double round(double value, int precision) {
      int scale = (int) Math.pow(10, precision);
      return (double) (Math.round(value * scale) / scale);
  }

See also, https://stackoverflow.com/a/22186845/212950 Thanks to jpdymond for offering this.

Edit: Added round brackets. Casts the whole result to double, not the first argument only!

Changing the summary title to be succinct. ;)
Source Link
MAbraham1
  • 1.8k
  • 4
  • 28
  • 45

Here's a basicA succinct solution offered by jpdymond:

   public static double round(double value, int precision) {
      int scale = (int) Math.pow(10, precision);
      return (double) Math.round(value * scale) / scale;
  }

See also, https://stackoverflow.com/a/22186845/212950 Thanks to jpdymond for offering this.

Here's a basic solution offered by jpdymond:

   public static double round(double value, int precision) {
      int scale = (int) Math.pow(10, precision);
      return (double) Math.round(value * scale) / scale;
  }

https://stackoverflow.com/a/22186845/212950

A succinct solution:

   public static double round(double value, int precision) {
      int scale = (int) Math.pow(10, precision);
      return (double) Math.round(value * scale) / scale;
  }

See also, https://stackoverflow.com/a/22186845/212950 Thanks to jpdymond for offering this.

Source Link
MAbraham1
  • 1.8k
  • 4
  • 28
  • 45

Here's a basic solution offered by jpdymond:

   public static double round(double value, int precision) {
      int scale = (int) Math.pow(10, precision);
      return (double) Math.round(value * scale) / scale;
  }

https://stackoverflow.com/a/22186845/212950

Morty Proxy This is a proxified and sanitized view of the page, visit original site.