tiddi rastogi
tiddi rastogi

Reputation: 526

JSTL formatnumber tag is not applying percentage type

I have to shown the difference between two numbers in both numerical format and percentage row in a single fields in such way(difference)(% difference).Difference in numerical format is being displayed but when I do type="percentage" ,then it is not displayed in percentage form .Why?? My code is-

<td scope="row" style="${r.st1_vs1_bag2_rb-row.st1_vs1_bag2_rb eq 0 ?  'background-color: lime':'background-color: pink'}">
<c:choose><c:when test="${(r.st1_vs1_bag2_rb-row.st1_vs1_bag2_rb) ne 0}"> 
<fmt:formatNumber value="${(r.st1_vs1_bag2_rb-row.st1_vs1_bag2_rb)}" maxFractionDigits="2" minIntegerDigits="2" pattern="##.##E0" var="mm" type="percent"></fmt:formatNumber>
${(mm)}
</c:when>
<c:otherwise>
${(r.st1_vs1_bag2_rb-row.st1_vs1_bag2_rb)}</c:otherwise></c:choose></td>

Upvotes: 0

Views: 1106

Answers (1)

alfreema
alfreema

Reputation: 1338

There could be two things going on.

1) With formatNumber you need to make a choice: Either use TYPE or use PATTERN, but don't use both at the same time. If you do, PATTERN will take precedence (it will be used and TYPE will be ignored). So if you choose to use TYPE, change your code to something like:

<fmt:formatNumber value="${(r.st1_vs1_bag2_rb-row.st1_vs1_bag2_rb)}" maxFractionDigits="2" minIntegerDigits="2" var="mm" type="percent"/>

I simply removed the PATTERN.

Or if you choose to use PATTERN then try (this depends on the nature of the value you are supplying):

<fmt:formatNumber value="${(r.st1_vs1_bag2_rb-row.st1_vs1_bag2_rb)}" pattern="##.##" var="mm"/>

2) Second, I noticed you are outputting the var "mm" with ${(mm)}. My guess is that allows the engine to interpret the value you are feeding into the ${..} as a number, processing it as such, and converting it to a string which could cause it to lose the format.

Either way, make sure you change that to:

${mm}

Note: If you can supply an example of the raw value being passed to formatNumber, we can more easily help you. For example, what does this display without formatting it?

${(r.st1_vs1_bag2_rb-row.st1_vs1_bag2_rb)}

Upvotes: 1

Related Questions