반응형
큰 수나, 형변환, 부동소수점 등의 문제를 회피하는데 도움이 됩니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigDecimal; | |
import java.math.MathContext; | |
import java.math.RoundingMode; | |
public class CalUtil { | |
public static String divideS(Object a, Object b, int scale) { | |
return newBigDecimal(a).divide(newBigDecimal(b), scale, RoundingMode.HALF_UP).toPlainString(); | |
} | |
public static Double divideD(Object a, Object b, int scale) { | |
return newBigDecimal(a).divide(newBigDecimal(b), scale, RoundingMode.HALF_UP).doubleValue(); | |
} | |
public static Long divideL(Object a, Object b) { | |
return newBigDecimal(a).divide(newBigDecimal(b), 0, RoundingMode.HALF_UP).longValue(); | |
} | |
public static Integer divideI(Object a, Object b) { | |
return newBigDecimal(a).divide(newBigDecimal(b), 0, RoundingMode.HALF_UP).intValue(); | |
} | |
public static Integer divideIF(Object a, Object b) { | |
return newBigDecimal(a).divide(newBigDecimal(b), 0, RoundingMode.FLOOR).intValue(); | |
} | |
public static Double subtractDR(Object a, Object b, int scale) { | |
return newBigDecimal(a).subtract(newBigDecimal(b), new MathContext(scale, RoundingMode.HALF_UP)).doubleValue(); | |
} | |
public static int multiplyI(Object a, Object b) { | |
return newBigDecimal(a).multiply(newBigDecimal(b)).intValue(); | |
} | |
public static BigDecimal newBigDecimal(Object a) { | |
if (a instanceof String) { | |
return new BigDecimal((String) a); | |
} | |
if (a instanceof Long) { | |
return new BigDecimal((Long) a); | |
} | |
if (a instanceof Integer) { | |
return new BigDecimal((Integer) a); | |
} | |
if (a instanceof Float) { | |
return new BigDecimal((Float) a); | |
} | |
if (a instanceof Double) { | |
return new BigDecimal((Double) a); | |
} | |
throw new RuntimeException(a.getClass().getName() + " is not supported."); | |
} | |
/** | |
* 직각 삼각형 아랫변 길이 구하기 | |
* | |
* @param slope 기울기 | |
* @param hypotenuse 빗변길이 | |
* @return | |
*/ | |
public static Double getTriangleBaseWidth(Double slope, Double hypotenuse) { | |
return Math.abs(CalUtil.divideD(hypotenuse, Math.sqrt(Math.pow(slope, 2) + 1), 8)); | |
} | |
/** | |
* 직각 삼각형 높이 길이 구하기 | |
* | |
* @param slope 기울기 | |
* @param hypotenuse 빗변길이 | |
* @return | |
*/ | |
public static Double getTriangleHeight(Double slope, Double hypotenuse) { | |
return Math.abs(CalUtil.divideD(hypotenuse * slope, Math.sqrt(Math.pow(slope, 2) + 1), 8)); | |
} | |
} |
반응형
'java' 카테고리의 다른 글
jackson JsonView (Json Filter) 사용법. (0) | 2022.10.07 |
---|---|
사용자 정의 JsonSerializer (0) | 2022.09.27 |
spring 최초 한번 실행. (0) | 2022.09.07 |
스프링부트 jackson response date 타입 변환 설정 (0) | 2022.09.02 |
java - 금액 및 숫자 한글로 변환 (0) | 2022.01.28 |
Springboot Web Mvc Config 설정 예제 (0) | 2021.10.05 |
springboot thymeleaf maven (0) | 2021.09.29 |
springboot thymeleaf false (0) | 2021.09.29 |