2

We have a jsp-page which presents some double values, on some of the doubles there is also a multiplication by -1 made on the jsp. This works fine except for zero values which is presented as "-0" and choose of correct css class fails in the el statement.

Here's an example to show the problem:

<c:set var="positiveZero" value="${0.0}" />
<c:set var="negativeZero" value="${0.0 * -1}" />

<p>negativeZero: ${negativeZero}</p>
<p>less than 0? ${negativeZero < 0}</p>

<p>positiveZero: ${positiveZero}</p>
<p>less than 0? ${positiveZero < 0}</p>

The output of this is:

negativeZero: -0.0
less than 0? true
positiveZero: 0.0
less than 0? false

Do you have any suggestion how to make sure the values are presented as "positive zeros" and also make sure that the "less than 0" statement returns false in both cases?

SubUrban
  • 23
  • 3
  • Try adding a zero to a negative zero. It should remove the minus sign. – Tiny Nov 17 '14 at 14:20
  • I would look into this solution: http://stackoverflow.com/questions/13749001/jsp-el-any-function-to-get-absolute-value-of-a-number – kaze Nov 17 '14 at 14:48
  • @Kaze, something similar to that could be a solution, but in this case I cannot use Math.ABS since in some occations the value is actually negative. – SubUrban Nov 18 '14 at 08:08

2 Answers2

0

Add 0.00.

Because:

(-0.0) + 0.0 -> 0.0

See here for the answer to a similar question and links to sources.

Community
  • 1
  • 1
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • Thanks! The only drawback with this solution is that the JSP code will look a bit more "messy" since this will be added about 10 times on one single jsp. Is there no other more nice looking solution? – SubUrban Nov 17 '14 at 14:47
  • @SubUrban - Not sure how to make totally clean - may be worthwhile adding a static method in some Java library - something like `${MathLib.asPositive(negativeZero)}` perhaps. – OldCurmudgeon Nov 17 '14 at 15:00
  • The solution was to add an additional getValueInverted() method in my java model object which multiply by -1 and add 0.0d. With that method I can remove the calculation from jsp layer and also JUnit-test it, cleaner and nicer:-) Thanks for help! – SubUrban Nov 18 '14 at 08:12
  • @SubUrban - I would have called it `negate` but that is your call. – OldCurmudgeon Nov 18 '14 at 09:52
0

you could use this little workaround:

${negativeZero < 0 ? -negativeZero : negativeZero}
Linora
  • 10,418
  • 9
  • 38
  • 49
  • This would not work in this particular case since some of the values actually are and should be presented as less than 0. – SubUrban Nov 17 '14 at 14:31