We’re rewarding the question askers & reputations are being recalculated! Read more.

 2 added 574 characters in body edited Apr 1 '15 at 23:08 TheCoffeeCup 9,33033 gold badges3131 silver badges8888 bronze badges Creating a new BigDecimal object is a bit expensive. Why don't you do: public static double round(double numberToRound, int numberOfDecimals) { int tenPow = Math.pow(10, numberOfDecimals); return Math.round(numberToRound * tenPow) / tenPow; }  This method is self explanatory: It first gets $$\10^n\$$ where $$\n\$$ is the number of decimals Then multiplies the number to round with the previous result, and rounds it to the nearest integer Finally divide $$\10^n\$$ from the result This way, you don't have to create a BigDecimal. You can further increase efficiency by making your own pow() method that only accepts a double and an int: public static double pow(double x, int toPow) { if (toPow == 1) { return x; } else if (toPow == 0) { return 1; } else if (toPow < 0) { return pow(x, toPow + 1) / x; } else { return pow(x, toPow - 1) * x; } }  Since Math.pow() also has to deal with stuff like $$\2^{1.5}\$$ and has to use a more complex algorithm to figure out the result, so a simple recursive algorithm can easily be more efficient than Math.pow(). Creating a new BigDecimal object is a bit expensive. Why don't you do: public static double round(double numberToRound, int numberOfDecimals) { int tenPow = Math.pow(10, numberOfDecimals); return Math.round(numberToRound * tenPow) / tenPow; }  This method is self explanatory: It first gets $$\10^n\$$ where $$\n\$$ is the number of decimals Then multiplies the number to round with the previous result, and rounds it to the nearest integer Finally divide $$\10^n\$$ from the result This way, you don't have to create a BigDecimal. Creating a new BigDecimal object is a bit expensive. Why don't you do: public static double round(double numberToRound, int numberOfDecimals) { int tenPow = Math.pow(10, numberOfDecimals); return Math.round(numberToRound * tenPow) / tenPow; }  This method is self explanatory: It first gets $$\10^n\$$ where $$\n\$$ is the number of decimals Then multiplies the number to round with the previous result, and rounds it to the nearest integer Finally divide $$\10^n\$$ from the result This way, you don't have to create a BigDecimal. You can further increase efficiency by making your own pow() method that only accepts a double and an int: public static double pow(double x, int toPow) { if (toPow == 1) { return x; } else if (toPow == 0) { return 1; } else if (toPow < 0) { return pow(x, toPow + 1) / x; } else { return pow(x, toPow - 1) * x; } }  Since Math.pow() also has to deal with stuff like $$\2^{1.5}\$$ and has to use a more complex algorithm to figure out the result, so a simple recursive algorithm can easily be more efficient than Math.pow(). 1 answered Mar 29 '15 at 16:56 TheCoffeeCup 9,33033 gold badges3131 silver badges8888 bronze badges Creating a new BigDecimal object is a bit expensive. Why don't you do: public static double round(double numberToRound, int numberOfDecimals) { int tenPow = Math.pow(10, numberOfDecimals); return Math.round(numberToRound * tenPow) / tenPow; }  This method is self explanatory: It first gets $$\10^n\$$ where $$\n\$$ is the number of decimals Then multiplies the number to round with the previous result, and rounds it to the nearest integer Finally divide $$\10^n\$$ from the result This way, you don't have to create a BigDecimal.