BigDecimal in Java
/**
* //performance tips
* //1 use BigDecimal.valueOf instead of new BigDecimal
* //2 use BigDecimal.stripTrailingZeros to avoid unnecessary trailing zeros
* //3 use BigDecimal.setScale to avoid unnecessary decimal places
* //4 use BigDecimal.compareTo instead of equals to avoid precision loss
*/
public static void main(String[] args) {
//1.1 String VS double as parameter of BigDecimal constructor
BigDecimal num =new BigDecimal(0.1);
//0.1000000000000000055511151231257827021181583404541015625
System.out.println(num);
num =new BigDecimal("0.1"); //0.1
System.out.println(num);
//1.2 valueOf VS new BigDecimal
BigDecimal num3 = BigDecimal.valueOf(0.1);
System.out.println("BigDecimal.valueOf(0.1): "+num3);//0.1
//2.1 scale is very important in divide of BigDecimal
BigDecimal num1 = new BigDecimal("10.0");
BigDecimal num2 = new BigDecimal("3.0");
BigDecimal result = num1.divide(num2, 2, RoundingMode.HALF_UP);
//2.2 java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
try {
result = num1.divide(num2); // throw exception
} catch (ArithmeticException e) {}
//3. equals vs compareTo (-1, 0, or 1 as this BigDecimal is numerically less than, equal to, or greater than val.)
BigDecimal num4 = new BigDecimal("1.0");
BigDecimal num5 = new BigDecimal("1.00");
System.out.println(num4.equals(num5)); //false
System.out.println(num4.compareTo(num5)); //0
//4.1 scale(小数位数)和 precision(总位数)
BigDecimal num6 = new BigDecimal("12345678901234567890.1234567890");
System.out.println(num6.scale()); //10
System.out.println(num6.precision()); //30
BigDecimal stripped=num6.stripTrailingZeros();
System.out.println(stripped.scale());//9
System.out.println(stripped.precision());//29
//4.2 setScale
BigDecimal num7 = new BigDecimal("1234567890.1234567890");
BigDecimal fixed=num7.setScale(2,RoundingMode.HALF_UP);
System.out.println(fixed);
//5.1 BigDecimal is stateless, so it is thread-safe.
// BigDecimal is immutable, so it is safe to use it as key of ConcurrentHashMap.
ConcurrentHashMap<BigDecimal, String> map = new ConcurrentHashMap<>();
// Adding key-value pairs
map.put(new BigDecimal("10.50"), "Item A");
map.put(new BigDecimal("25.75"), "Item B");
map.put(new BigDecimal("15.00"), "Item C");
// Retrieving values
String itemA = map.get(new BigDecimal("10.50"));
System.out.println("Item A: " + itemA);
// Iterating over the map (not thread-safe if concurrent modifications occur)
for (BigDecimal key : map.keySet()) {
System.out.println(key + ": " + map.get(key));
}
//5.2 sum of BigDecimal
BigDecimal sum = new BigDecimal("0.0");
for (BigDecimal value : map.keySet()) {
sum = sum.add(value);
sum.add(value); // error: sum is immutable
}
System.out.println("Sum of all values: " + sum);
//6 which better, BigDecimal or double?
BigDecimal principal=new BigDecimal("10000");
BigDecimal rate=new BigDecimal("0.05");
BigDecimal interest=principal.multiply(rate);
System.out.println("Interest: "+interest);
double principal2=10000;
double rate2 =0.05;
BigDecimal interest2=BigDecimal.valueOf(principal2*rate2);
System.out.println("Interest2: "+interest2);
}
评论
发表评论