Good evening. Again. It's been a long time since my last post. .. ..

In this article, I will describe how to use BigDecimal, which makes the calculation after the decimal point accurate in Java. I will not touch on why the calculation after the decimal point can be done accurately, so I hope you can find out by yourself. I hope it will be helpful for those who want to calculate after the decimal point accurately.

Now, let's write about the basic Big Decimal description method. To use BigDecimal

`.java`

```
import java.math.BigDecimal;
```

Need to be imported. Since it is a standard library, there is no need to bring it from the outside and it can be used immediately. The description method and usage example are shown below.

Four arithmetic operations | Description method |
---|---|

Addition | add |

Subtraction | subtract |

Multiply | multiply |

division | divide |

`Main.java`

```
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
//Value definition
int one = 1;
int two = 2;
BigDecimal three = BigDecimal.valueOf(3);
BigDecimal five = BigDecimal.valueOf(5);
//Basic
System.out.println(three.add(five));//8
System.out.println(three.subtract(five));//-2
System.out.println(three.multiply(five));//15
System.out.println(three.divide(five));//0.6
//A little applied
System.out.println(BigDecimal.valueOf(two).add(BigDecimal.valueOf(one)).multiply(three));//9
}
}
```

Such a description is possible. Also, as shown in the application of the comment part, it is possible to write several times in a row. (I used Big Decimal for half a year without knowing this ...)

From here, the calculation handles the decimal point. Since the description of ROUND_UP etc. is deprecated in Java9 or later, please use the description of UP etc. used in this article. BogDecimal allows you to truncate and round off decimal values. Use the setScale method to do this. In the first argument, describe the number of decimal places to control. For example, if the first argument of setscale is 0, it controls to the first decimal place. The description method and usage example are described below.

Rounding method | Description method |
---|---|

Rounding | HALF_UP |

Round up | UP |

Truncate | DOWN |

`Main.java`

```
import java.math.BigDecimal;
import java.math.RoundingMode;
public class Main {
public static void main(String[] args) {
//Value definition
BigDecimal one = BigDecimal.valueOf(1);
BigDecimal three = BigDecimal.valueOf(3);
BigDecimal num = new BigDecimal("1.567");
//Basic
System.out.println(num.setScale(0,RoundingMode.HALF_UP));//2
System.out.println(num.setScale(1,RoundingMode.DOWN));//1.5
System.out.println(num.setScale(2,RoundingMode.UP));//1.57
//A little applied
System.out.println(one.divide(three).setScale(2,RoundingMode.DOWN));//error
System.out.println(one.divide(three,2,RoundingMode.DOWN));//0.3
}
}
```

The variable declaration and assignment on the 3rd line of the value definition are different from the 1st and 2nd lines. Regarding this, even if you use BigDecimal, you may not be able to handle the decimal point perfectly. As a countermeasure against this, when using a decimal point in BigDecimal, it is better to handle it as a String. Although it is a String type, it will calculate as it is.

I made a special description in the second line of the application. I will explain about that. When a recurring decimal occurs in division of the BigDecimal type (0.3333 in this case ...) I get an exception for java.lang.ArithmeticException. As a countermeasure, Big Decimal allows you to write something like the second line of application. With this description, it is possible to write a program without raising an exception.

After you finish calculating the decimal point in BigDecimal, you may want to use that value in a primitive type. Therefore, finally, I will describe how to convert from BigDecimal type to int type or double type. Int type and double type are shown as examples.

The type you want to convert | Description method |
---|---|

int | intValue() |

double | doubleValue() |

`Main.java`

```
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
//Value definition
BigDecimal one = BigDecimal.valueOf(1);
BigDecimal num = new BigDecimal("1.567");
int a;
double b;
//Type conversion
a = one.intValue();
b = num.doubleValue();
}
}
```

By writing in this way, it is possible to convert the value calculated by BigDecimal to a primitive type.

This time, I wrote about the basic description of BigDecimal, which is often used to accurately calculate decimal points in Java. There are many other useful methods such as big / small comparison of Big Decimal and exponentiation, so please check them out. If you have an opinion that you would like to know a little more, I may add an article. (I have a desire to write, but it depends on how busy I am ...) If there are any mistakes, strange expressions, or parts that can be described more clearly, it will be my study, so please comment. Thank you for reading this far.

Recommended Posts