About the treatment of BigDecimal (with reflection)

About Java Big Decimal

I think there are many situations where java is used in development work. I think that Big Decimal may be used when building calculation logic. Suddenly, it's a different type from a normal integer, so I was confused, so I decided to leave it in the article.

What is Big Decimal

https://docs.oracle.com/javase/jp/8/docs/api/java/math/BigDecimal.html> From oracle.docs No, I don't really understand even if it is said mathematically. .. .. So, in other words ... In the case of calculations where floats and doubles cause errors, unintended results may be obtained. For example

double.java


 double result = 10d / 6d;
 System.out.println(result)
 //Output 1.6666666666666666666666666666666666666666

It's like that. As you may know, if you want to calculate "bank rounding", the above example is too irregular and you will be in trouble. That's where Big Decimal comes in. It is often used for so-called core banking calculations.

BigDecimal is different from the normal number calculation and calculates as follows.

BigDecimalCalc.java


//When dividing 1000 of BigDecimal type by 100 of int type
   BigDecimal bigNum = BigDecimal.valueOf(1000);
   BigDecimal result = bigNum.divide(BigDecimal.valueOf(100), 2, RoundingMode.DOWN); 
   System.out.println(result);

Use a special method for divide. Normal / cannot be used. See oracle.docs for details.

When comparing with 0

How to handle 0 of BigDecimal in conditional branching etc. I'm addicted to this if I'm not used to it. Of course I was addicted to it because I am also a beginner.

ZeroCompare.java


 if(Value to compare.compareTo(BigDecimal.ZERO) == 0) {
  //・ ・ ・
 }

It looks like. In other words, there is no difference compared to 0, so 0! It will be a comparison. By the way, if it is 0 or more or 0 or less

Compare.java


ZeroCompare.java
 if(Value to compare.compareTo(BigDecimal.ZERO) > 0) {
  //・ ・ ・
 } 

To do.

Summary

BigDecimal is used differently than primitive types. It is necessary to be aware of that. Especially for java beginners like me, it's hard to accidentally use floats and doubles when you have to make accurate calculations in actual development work, or to use BigDecimal incorrectly and cause compilation errors. Don't do it. You may not usually care about it, but in the case of work, I would like to listen carefully to the purpose of the calculation logic to be developed and the required accuracy before developing it. [Reflection]

Recommended Posts

About the treatment of BigDecimal (with reflection)
About the handling of Null
About the description of Docker-compose.yml
[Jackson] A story about converting the return value of BigDecimal type with a custom serializer.
About the behavior of ruby Hash # ==
About the basics of Android development
About the role of the initialize method
Think about the 7 rules of Optional
Summary about the introduction of Device
About the log level of java.util.logging.Logger
A story about hitting the League Of Legends API with JAVA
About the version of Docker's Node.js image
Check the contents of params with pry
What is testing? ・ About the importance of testing
About the operation of next () and nextLine ()
About the initial display of Spring Framework
About the error message Invalid redeclaration of'***'
About the number of threads of Completable Future
About the mechanism of the Web and HTTP
Format the contents of LocalDate with DateTimeFormatter
Think about the combination of Servlet and Ajax
Verify the contents of the argument object with Mockito
About the description order of Java system properties
Manage the version of Ruby itself with rbenv
The story of tuning android apps with libGDX
About the method
Calculate the similarity score of strings with JAVA
Prepare the environment of CentOS 8 with Sakura VPS
Specify the default value with @Builder of Lombok
Measure the distance of the maze with breadth-first search
About the package
I checked the number of taxis with Ruby
About the mechanism when displaying the GUI with Docker
[Swift] Get the number of steps with CMP edometer
List the contents of categories created with Active Hash
JavaFX --Match the size of ImageView with other nodes
[Grails] About the setting area and the setting items of application.yml
[JUnit5] Dealing with "the reference of assertEquals is ambiguous"
About the usefulness of monads from an object-oriented perspective
Access the built-in h2db of spring boot with jdbcTemplate
About the problem of deadlock in parallel processing in gem'sprockets' 4.0
Test the contents of an Excel file with JUnit
The story of making a reverse proxy with ProxyServlet
Monitor the internal state of Java programs with Kubernetes
Implement the UICollectionView of iOS14 with the minimum required code.
[Technical memo] About the advantages and disadvantages of Ruby
Check the result of generic parameter inference with JShell
Roughly the flow of web application development with Rails.
I learned about the existence of a gemspec file
Control the processing flow of Spring Batch with JavaConfig.
The story of making dto, dao-like with java, sqlite
Replace only part of the URL host with java
Output about the method # 2
About the StringBuilder class
The world of clara-rules (2)
Commentary: About the interface
About disconnect () of HttpURLConnection
About the asset pipeline
About the function double-java
About selection of OpenJDK
About DI of Spring ①