Mohamed Nuur
Mohamed Nuur

Reputation: 5655

fmt:formatNumber how to display deltas (+/-)

I want to use fmt:formatNumber to display the following:

  1. .8 => 80%
  2. -.8 => -80%
  3. 1721 => 1,721
  4. 1721 => +1,721
  5. -7876 => -7,876

UPDATE: The format works perfectly for percent and grouping, but it doesn't work so well for putting a + in front of a number, as in the following:

  1. .8 => +80%
  2. 1721 => +1,721

How can I do this?

Upvotes: 3

Views: 1579

Answers (1)

Behrang Saeedzadeh
Behrang Saeedzadeh

Reputation: 47951

You can use the % pattern to "multiply by 100 and show as percentage". I think in your case you would need something like this:

// if num < 1 && num > -1 (or another logic)
<fmt:formatNumber value="${num}" format="%" />
// else
<fmt:formatNumber value="${num}" format="'+'###,###;'-'###,###" /> // show sign

UPDATE: You can use this method as well:

// if num < 1 && num > -1 (or another logic)
<fmt:formatNumber value="${num}" type="percentage" />
// else
<fmt:formatNumber value="${num}" type="number" groupingUsed="," />

Upvotes: 6

Related Questions