JSTL arithmetic losing precision -
this question has answer here:
- is floating point math broken? 20 answers
why jstl block resulting 0.9999999999999999, , fix?
<c:set var="one" value="0.1"/> <c:set var="two" value="0.7"/> <c:set var="three" value="0.1"/> <c:set var="four" value="0.1"/> <c:out value="${one+two+three+four}"/>
jb nizet's comment correct, here's how deal it.
you should consider using fmt:formatnumber function when dealing floating point numbers. here url: http://www.tutorialspoint.com/jsp/jstl_format_formatnumber_tag.htm
and here example:
<c:set var="one" value="0.1"/> <c:set var="two" value="0.7"/> <c:set var="three" value="0.1"/> <c:set var="four" value="0.1"/> <fmt:formatnumber value="${one+two+three+four}" maxfractiondigits="2" />
make sure include line @ top of jsp code:
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
Comments
Post a Comment