this stackoverflow post을 기준으로 다음과 같이 0.59가 아니라 0.60을 출력 할 것으로 예상됩니다.DecimalFormat/RoundingMode.Down 기능에 혼란 스럽습니다. 가장자리 케이스의 경우
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class Test {
public static void main(String[] args) {
double toFormat = 0.6;
DecimalFormat formatter = new DecimalFormat("########0.00");
formatter.setRoundingMode(RoundingMode.DOWN);
System.out.println(formatter.format(toFormat)); // 0.60
}
}
0.60의 가장 가까운 부동 소수점 표현은 다음과 0.6 인 0.59999999999999997779553950749686919152736663818359375이다. Java 8에서 DecimalFormat을 RoundingMode.DOWN으로 설정 한 경우 왜이 값을 0.59로 반올림하지 않습니까?