Switch to full style
General Java code examples
Post a reply

Scale double number using big decimal

Mon Apr 22, 2013 10:51 pm

Scale double number using big decimal. E.g. Scaling 42.26113 with 2 leads to 42.26.
java code
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;

public final class DecimalScaler {

private DecimalScaler() {
}

public static BigDecimal scaleBigDecimal(double val, int scale) {
BigDecimal value = new BigDecimal(Double.toString(val));
value = value.setScale(scale, RoundingMode.HALF_UP);

return value;
}


public static BigDecimal scaleBigDecimal(BigDecimal val, int scale) {
BigDecimal ret = val.setScale(scale, RoundingMode.HALF_UP);
return ret;
}
}




Post a reply
  Related Posts  to : Scale double number using big decimal
 convert decimal number to octal number     -  
 convert octal number to decimal number     -  
 convert binary number to decimal     -  
 Image Scale in java     -  
 convert integer number to octal,hexadecimal number systems     -  
 add sequence of decimal numbers     -  
 convert hexadecimal to decimal     -  
 conversion from binary to decimal numbers as string     -  
 displaying double quotes in ASP     -  
 How to set double fraction Precision     -  

Topic Tags

Java Variables