문제점 : JSF를 통해 BigDecimal을 형식화해야하지만 JSF는 BigDecimal의 정밀도를 파괴합니다.JSF 부정확 한 BigDecimal 변환
JSF :
<h:outputText value="#{webUtilMB.roundUp(indexPrice.percentage, 2)}"/>
자바 : WebUtilManagedBean.roundUp 방법에 브레이크 포인트를 갖는
public class IndexPrice {
public BigDecimal getPercentage(){ return new BigDecimal("1.325"); }
}
@ManagedBean("webUtilMb")
public class WebUtilManagedBean{
public BigDecimal roundUp(BigDecimal dbvalue, int scale){
return dbvalue.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
}
}
날 'dbvalue는'1.325 '하지'1.3249999999999999555910790149937383830547332763671875 '이고 보여 주었다. 이 오버로드 된 메서드에 중단 점을 가진 때 나를 놀라게 무엇
public Double roundUp(Double dvalue, int scale){
System.out.println(dvalue);
}
했다 :
나는 다음과 WebUtilManagedBean에 검거 방법을 오버로드 - 'dvalue는'1.325 '이고, 올바른이다. - 메서드가 실제로 roundUp (BigDecimal, int) 메서드 대신 호출되었습니다.
나중에 다음과 같은 결과와는 BigDecimal의 생성자 실험 :
BigDecimal db1 = new BigDecimal("1.325"); -> 1.325
BigDecimal db2 = new BigDecimal(1.325d); -> 1.3249999999999999555910790149937383830547332763671875
이론 : JSF 더블보다는, 문자열로 변환 내 BigDecimal의 값을 복용했다는 위의에서, 그것은 보인다 값에 새로운 BigDecimal (double)를 호출 해 BigDecimal를 취득 해, 잘못된 값을 돌려줍니다.
는수정 :이 문제를 해결하는 한 가지 방법은 다음 코드를 사용하는 것입니다
@ManagedBean("webUtilMb")
public class WebUtilManagedBean{
public Double roundUp(Double dvalue, int scale){
return this.roundUp(**new BigDecimal(dvalue.toString())**, BigDecimal.ROUND_HALF_UP);
}
public BigDecimal roundUp(BigDecimal dbvalue, int scale){
return dbvalue.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
}
}
을하지만 그건 그냥 나에게 해킹처럼 보인다.
수정 사항 및 그 이유에 대한 아이디어. 감사.