개발/Java
[Java] 소수점 처리하는 방법
wwwnghks
2020. 2. 18. 15:14
자바 소숫점 처리하는 방법
String.format 은 반올림을 해서 반환해준다.
double test = 12.3456789;
System.out.println(String.format("%.2f", test)); //결과 : 12.35
System.out.println(String.format("%.3f", test)); //결과 : 12.346
기존에는 Math.round 로 소수점 처리하는 방법을 알아봤다. Math 클래스로 사용하는 방법은 아래 링크에서 확인하면 된다.
Math.round와 차이점은
Math.round한 결과가 10.1 이라면 10.1 로 나오지만, String.format은 10.10 으로 자릿수를 모두 채워서 반환해준다.
https://wwwnghks.tistory.com/76
[Java] 자바 반올림,올림,버림 함수(ROUND, CEIL, FLOOR) 사용법
자바 반올림,올림,버림 함수(ROUND, CEIL, FLOOR) 사용법 각각 반올림(round), 올림(ceil), 버림(floor) 해줄 수 있는 함수입니다. 반올림 함수 (ROUND) double test = Math.round(12.345*100)/100 // 12.35 올림..
wwwnghks.tistory.com