Skip to content

Instantly share code, notes, and snippets.

@zeraf29
Created January 8, 2019 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeraf29/3109ef8012f220c8ebb676465cf864da to your computer and use it in GitHub Desktop.
Save zeraf29/3109ef8012f220c8ebb676465cf864da to your computer and use it in GitHub Desktop.
for Double Multiply
import java.math.*;
public class Multiply {
public static Double multiply(Double a, Double b) {
BigDecimal s1 = new BigDecimal(String.valueOf(a));
BigDecimal s2 = new BigDecimal(String.valueOf(b));
//return a * b
return s1.multiply(s2).doubleValue();
}
}
@zeraf29
Copy link
Author

zeraf29 commented Jan 8, 2019

[Java]
Double 연산의 경우, 컴퓨터가 해당 값을 정확하게 표현을 하지 못하여 예상치 못한 결과가 나올 때가 있다.
(예: System.out.println(7.12*(0.21));//1.4952 이지만 1.4951999999999999 출력)
이러한 결과를 방지하기 위해 BigDecimal 타입을 이용하여 계산해야 한다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment