views:

372

answers:

1

Hi,

i want to do arithmetic operation on jsp . I am using struts tag lib tag

following is the code :

    <s:set name="value1" value ="%{0.0}" />
    <s:set name="value2" value ="%{0.0}" />
    <s:set name="percent" value ="%{0.0}" />

    <s:iterator>
          <s:set name="value1" value ="%{#value1+ someIntegerValue1}" />
          <s:set name="value2" value ="%{#value2+ someIntegerValue2}" />
    </s:iterator>

    <s:set name="percent" value ="%{(#value1*100.0)/#value2}" />
<fmt:formatNumber type="number" maxFractionDigits="2" minFractionDigits="2" value="${percent}" />

now the last line always rounds to integer value

like if value1 = 3 , value2 = 31 . then percent should be equal to (3*100)/31 = 9.68

BUT the damn thing outputs to 9.00

<s:set name="percent" value ="%{(3*100.0)/31.0}" />
<fmt:formatNumber type="number" maxFractionDigits="2" minFractionDigits="2" value="${percent}" />

then it outputs correctly 9.68 :S

can anyone please help ?? Now when i hardcode this expression

I have been banging my head on this for a long time, googling for more than 2-3 hours didnt get me anything .

+2  A: 

value2 should be a double to get it to work. 31 is a long in EL and 31.0 is a double in EL.

That said, JSP is more intented for presentation, not for some arithmetic stuff. More clean (and less headbanging) way would be to just do the math in a bean and access the outcome the usual EL way.

BalusC
well the someIntegerValue1 and someIntegerValue2 were added up in value1 and value2 . and there is "no" way (i have been banging my head against it) to cast or force jsp exptression to give floting point answer from an expression of "non-floating" points jsp variable. At the end i had to change the bean and change the type of someIntegerValue1 and someIntegerValue2 to Double . that did solved it and ethe answer came out correct. Thanks for the response BalusC
Ali