Jstl negative zero

If you try the next bit, you get -0

<c:set var="demo" value="-0.04" />
<fmt:formatNumber maxFractionDigits="1" value="${demo}" var="demo" />

but if you check if less than 0 it says true

<c:if test="${demo < 0.00}">...</c:if>

How can I get around this? It does not seem to make sense, 0 is -0, I proved it in algebra ... In this post they point to the IEEE link, but still, I can not solve the problem

EDIT: Thanks for the comment, there is an error in the sample code. I assign a rounded value to a demo variable. And the output is not 0, but -0, I tried it

+5
source share
3 answers

The long shot is here, but is it because it parses the original -0.04 as a string?

Perhaps try this, which I think will make it evaluate as a number:

<c:set var="demo" value="${-0.04}" />
0
source

, , : ( ) java.text.DecimalFormat, <fmt:formatNumber /> .

"" Beanshell, :

BeanShell 2.0b4 - by Pat Niemeyer (pat@pat.net)
bsh % import java.text.*;
bsh % fmt = new DecimalFormat();
bsh % fmt.setMaximumFractionDigits(1);
bsh % print(fmt.format(-0.04d));
-0
bsh % 
0

, .

// Read these values from tag attributes
String pattern = "0";
Double myNumber = -0.04d;

// Create a decimal format with correct rounding
DecimalFormat df = new DecimalFormat(pattern);
df.setRoundingMode(RoundingMode.HALF_UP);

// Format in two steps to compensate for '-0'
String temp = df.format(myNumber + 1);
String formatted = df.format(Double.valueOf(temp) - 1);
0
source

All Articles