2

is it possible to use f:convertNumber to round up? I checked the api here and it didn't mention anything about rounding. If not, what is the next best thing to convert a double to a $ value while rounding it up?

<f:convertNumber maxFractionDigits="2" groupingUsed="true" currencySymbol="$" maxIntegerDigits="7" type="currency" />

Ex: $1.104999 should become $1.11

Steve
  • 11,831
  • 14
  • 51
  • 63

3 Answers3

1

You really shouldn't be using a primitive double in Java when dealing with exact numbers such as monetary amounts; rather, use java.lang.BigDecimal or some custom Money type; for an explanation why, see this SO question.

BigDecimal has support for several rounding modes; the one you are looking for is probably java.math.RoundingMode.UP.

As for the question on how to combine this with a f:convertNumber, I'm looking into that myself currently.

Community
  • 1
  • 1
László van den Hoek
  • 3,955
  • 1
  • 23
  • 28
1

This works for my specific case. But will it have any other edge cases that will break?

First, round it in my java class:

private double roundCost(double cost) {
    return (Math.ceil(cost*100))/100;
}

Then past that to my f:convertNumber.

I'm open to other suggestions.

Steve
  • 11,831
  • 14
  • 51
  • 63
0

One can add 0,005 (in your case) before using the default-rounding-mechanism.

Wiebke
  • 1,006
  • 8
  • 13