Steve
Steve

Reputation: 12393

JSF f:convertNumber round up currency

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

Upvotes: 2

Views: 5277

Answers (3)

Wiebke
Wiebke

Reputation: 1006

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

Upvotes: 0

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.

Upvotes: 1

Steve
Steve

Reputation: 12393

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.

Upvotes: 1

Related Questions