title: Dealing with BigDecimals in JSF
body: <p>I have one of the following <code>BigDecimal</code> converters.</p>
  
  <pre><code>@ManagedBean
  @ApplicationScoped
  public final class BigDecimalGeneralConverter implements Converter {
  
      private static final int scale = IntegerConstants.MAX_FRACTIONAL_DIGITS.getValue();
  
      @Override
      public Object getAsObject(FacesContext context, UIComponent component, String value) {
          if (!StringUtils.isNotBlank(value)) {
              return null;
          }
  
          BigDecimal bigDecimal = null;
          try {
              bigDecimal = new BigDecimal(value);
              if (bigDecimal.scale() &gt; scale) {
                  bigDecimal = new BigDecimal(value).setScale(scale, RoundingMode.HALF_UP);
              }
          } catch (NumberFormatException e) {
              throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "", Utility.getMessage("bigdecimal.converter.error", 123.45)), e);
          }
  
          return bigDecimal.stripTrailingZeros();
      }
  
      @Override
      public String getAsString(FacesContext context, UIComponent component, Object value) {
  
          if (value == null) {
              return "";
          }
  
          if (!(value instanceof BigDecimal)) {
              //The exception occurs here.
              throw new ConverterException(Utility.getMessage("bigdecimal.converter.error", 123.45));
          }
  
          DecimalFormat formatter = (DecimalFormat) NumberFormat.getCurrencyInstance();
          DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols();
          symbols.setCurrencySymbol("");
          formatter.setGroupingUsed(false);
          formatter.setDecimalFormatSymbols(symbols);
          return formatter.format((BigDecimal) value);
      }
  }
  </code></pre>
  
  <p>When this converter is used as follows,</p>
  
  <pre><code>&lt;h:outputText value="#{10.00}" converter="#{bigDecimalGeneralConverter}"/&gt;
  </code></pre>
  
  <p>it causes the following exception to be thrown.</p>
  
  <pre><code>SEVERE:   Error Rendering View[/admin_side/Test.xhtml]
  javax.faces.convert.ConverterException: Failed to convert the value as a BigDecimal ex, 123.45.
      at converter.BigDecimalGeneralConverter.getAsString(BigDecimalGeneralConverter.java:58)
      at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getFormattedValue(HtmlBasicRenderer.java:521)
      at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getFormattedValue(HtmlBasicRenderer.java:540)
      at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:357)
      at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)
      at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
      at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:456)
      at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133)
      at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
      at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
      at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
      at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
      at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
      at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:70)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
      at filter.NoCacheFilter.doFilter(NoCacheFilter.java:28)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
      at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
      at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
      at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
      at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
      at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
      at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
      at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
      at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
      at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
      at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
      at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
      at java.lang.Thread.run(Thread.java:722)
  
  INFO:   Exception when handling error trying to reset the response.
  javax.faces.convert.ConverterException: Failed to convert the value as a BigDecimal ex, 123.45.
      at converter.BigDecimalGeneralConverter.getAsString(BigDecimalGeneralConverter.java:58)
      at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getFormattedValue(HtmlBasicRenderer.java:521)
      at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getFormattedValue(HtmlBasicRenderer.java:540)
      at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:357)
      at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)
      at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
      at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:456)
      at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133)
      at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
      at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
      at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
      at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
      at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
      at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:70)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
      at filter.NoCacheFilter.doFilter(NoCacheFilter.java:28)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
      at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
      at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
      at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
      at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
      at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
      at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
      at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
      at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
      at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
      at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
      at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
      at java.lang.Thread.run(Thread.java:722)
  </code></pre>
  
  <p>The exception occurs in the <code>getAsString()</code> method as indicated by a comment. The number is not recognized as a <code>BigDecimal</code> value.</p>
  
  <p>This only works, when a property of type <code>BigDecimal</code> is used from the associated backing bean/model as follows.</p>
  
  <pre><code>&lt;h:outputText value="#{order.discountAmount}"
                converter="#{bigDecimalGeneralConverter}"/&gt;
  </code></pre>
  
  <p>I actually need to put a condition like as follows.</p>
  
  <pre><code>&lt;h:outputText value="#{not empty order.discountAmount ? order.discountAmount : 0}"
                converter="#{bigDecimalGeneralConverter}"/&gt;
  </code></pre>
  
  <p>This causes the above exception, when the <code>discountAmount</code> property in the <code>Order</code> entity is <code>null</code>.</p>
  
  <p>Why does it not recognize a literal value as a <code>BigDecimal</code> value?</p>