0

I encounter some weird rounding issue in JSF using below JSF code as numbers don't add up correctly:

<h:outputLabel value="Subtotal"/><h:outputText value="#{shoppingCart.itemsPrice}"><f:convertNumber groupingUsed="true" type="currency" currencySymbol="€"/></h:outputText>
<h:outputLabel value="Shipping"/><h:outputText value="#{shoppingCart.shippingPrice}"><f:convertNumber groupingUsed="true" type="currency" currencySymbol="€"/></h:outputText>
<h:outputLabel value="Vfat"/><h:outputText value="#{shoppingCart.vfat}"><f:convertNumber groupingUsed="true" type="currency" currencySymbol="€"/></h:outputText>
<h:outputLabel value="Total"/><h:outputText value="#{shoppingCart.totalGrossPrice}"><f:convertNumber groupingUsed="true" type="currency" currencySymbol="€"/></h:outputText>

here the numbers in question

  • Subtotal €49.50
  • Shipping €0.00
  • Vfat €9.40
  • Total €58.91

These numbers are not quite right sine VFAT in my country is 19% and thus VFat is actually 9,405 which I validated to be the value that is calculated. Displayed it should be 9,41 but it shows only 9,40 above. Even weirder however is that adding 49,50 to it gives the right 49,50+9,405 = 58,91 (rounded for display). So obviously this would be very confusing for a customer where the missing cent comes from. Obviously there may be cases where this cannot be avoided e.g. Vfat is ?,006 and ItemPrice is ?,004 then summed up this ?,01 but these is not even the case here.

Any ideas?

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
TomFree
  • 1,091
  • 3
  • 17
  • 31
  • 1
    Try using BigDecimal with proper precision. http://stackoverflow.com/questions/1359817/using-bigdecimal-to-work-with-currencies – Ravi Jul 26 '16 at 18:52
  • Thanks Ravi. With BigDecimal I get consistent results i.e. 0,005 is rounded down both for vfat as well as for gross price. And as I checked this is also legal to round this way in my country for vfat – TomFree Jul 27 '16 at 09:23

2 Answers2

2

You must scale decimal numbers before displaying them, preferably using java.math.BigDecimal type:

public BigDecimal getVFat2() {
    return vFat.setScale(2, BigDecimal.ROUND_UP);
}

HTML:

<h:outputText value="#{shoppingCart.vFat2}">
    <f:convertNumber groupingUsed="true" type="currency" currencySymbol="€" />
</h:outputText>
Victor T.
  • 121
  • 3
0

The rounding issue is not within JSF, it's most likely within your backing bean...as @Ravi states:

"Try using BigDecimal with proper precision."

Hatley
  • 217
  • 2
  • 11
  • It's not a backing bean issue. It's really the display. Calculaton is correct - I had written the net value, vfat and gross value to my log and validated that initially. here the log: [2016-07-27T10:53:19.549+0200] [glassfish 4.1] [INFO] ... [[ vfat price :9.405]] [2016-07-27T10:53:19.549+0200] [glassfish 4.1] [INFO] ...[[ net price :49.5]] [2016-07-27T10:53:19.554+0200] [glassfish 4.1] [INFO]... [[ total price :58.905]] Will try using BigDecimal anyway as for stronger calculations Ravi definitely has a valid point. – TomFree Jul 27 '16 at 09:02