We’re rewarding the question askers & reputations are being recalculated! Read more.
2 added 574 characters in body
source | link

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:

  1. It first gets \$10^n\$ where \$n\$ is the number of decimals
  2. Then multiplies the number to round with the previous result, and rounds it to the nearest integer
  3. 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:

  1. It first gets \$10^n\$ where \$n\$ is the number of decimals
  2. Then multiplies the number to round with the previous result, and rounds it to the nearest integer
  3. 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:

  1. It first gets \$10^n\$ where \$n\$ is the number of decimals
  2. Then multiplies the number to round with the previous result, and rounds it to the nearest integer
  3. 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
source | link

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:

  1. It first gets \$10^n\$ where \$n\$ is the number of decimals
  2. Then multiplies the number to round with the previous result, and rounds it to the nearest integer
  3. Finally divide \$10^n\$ from the result

This way, you don't have to create a BigDecimal.