How to use Big Decimal

I was writing a code that handles decimal numbers, and I got into it in various ways, so a memo at that time.

What happened

I needed to divide a decimal value, so I wrote the code to divide using BigDecimal as follows.

public class Divide {
	public static void main(String[] args) {
		BigDecimal num1 = new BigDecimal(1.1);
		BigDecimal num2 = new BigDecimal(100);

		System.out.println(num1.divide(num2));
	}
}

Since it is 1.1 ÷ 100, the result should be 0.011, but when I execute it, it outputs "0.01100000000000000088817841970012523233890533447265625". I wondered why, but it wasn't used properly.

What was happening

I took 1.1 as an argument when generating BigDecimal, but it seems that this one already contains an error. Since decimal decimals are represented by binary decimals, there are some values that cannot be represented in the first place. If you think about it, it's natural. .. .. Since 1.1 is a value that cannot be expressed, if BigDecimal is generated based on it, the error will be retained.

What should I do

Avoid using double and float values as shown below.

public class Divide {
	public static void main(String[] args) {
		BigDecimal num1 = new BigDecimal("1.1");
		BigDecimal num2 = new BigDecimal("100");

		System.out.println(num1.divide(num2));
	}
}

This will give you an accurate calculation. Then, does BigDecimal need a constructor that takes a double as an argument?

Further fit

ArithmeticException occurs when calculating an indivisible value as shown below.

public class Divide {
	public static void main(String[] args) {
		BigDecimal num1 = new BigDecimal("1.1");
		BigDecimal num2 = new BigDecimal("3");

		System.out.println(num1.divide(num2));
	}
}

BigDecimal's divide method seems to raise an exception if it is not divisible by default. Therefore, specify the number of digits and the rounding method as shown below.

public class Divide {
	public static void main(String[] args) {
		BigDecimal num1 = new BigDecimal("1.1");
		BigDecimal num2 = new BigDecimal("3");

		System.out.println(num1.divide(num2,2,RoundingMode.FLOOR));
	}
}

Recommended Posts

How to use Big Decimal
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[Java] How to use Map
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use Ruby return
[Rails] How to use enum
How to use @Builder (Lombok)
How to use java class
How to use Swift UIScrollView
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use String [] args
[Java] How to use string.format
How to use rails join
How to use Java Map
[Java] Use Big Decimal properly ~ 2018 ~
Ruby: How to use cookies
How to use dependent :: destroy
How to use Eclipse Debug_Shell
How to use Apache POI
[Rails] How to use validation
How to use Java variables
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
How to use GC Viewer
[Java] How to use Optional ①
How to use Lombok now
[Creating] How to use JUnit
[Rails] How to use Scope
How to use the link_to method
[Rails] How to use gem "devise"
How to use Lombok in Spring
How to use StringBurrer and Arrays.toString.
How to use arrays (personal memorandum)
How to use Java HttpClient (Get)