Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9232307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:16:59+00:00 2026-06-18T06:16:59+00:00

Problem: Need to format a BigDecimal through JSF, but JSF is destroying the precision

  • 0

Problem: Need to format a BigDecimal through JSF, but JSF is destroying the precision of the BigDecimal.

JSF:

<h:outputText value="#{webUtilMB.roundUp(indexPrice.percentage, 2)}"/>

Java:

public class IndexPrice {
  public BigDecimal getPercentage(){ return new BigDecimal("1.325"); }
}

@ManagedBean("webUtilMb")
public class WebUtilManagedBean{
  public BigDecimal roundUp(BigDecimal dbvalue, int scale){
    return dbvalue.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
  }
}

Having a break point in the WebUtilManagedBean.roundUp method showed me ‘dbvalue’ is ‘1.3249999999999999555910790149937383830547332763671875’ and not ‘1.325’.

I then overloaded the roundUp method in WebUtilManagedBean with:

public Double roundUp(Double dvalue, int scale){
  System.out.println(dvalue);
}

What surprised me when having a break point in this overloaded method was:
– ‘dvalue’ is ‘1.325’, which is correct.
– the method actually got called instead of the roundUp(BigDecimal, int) method.

I later experimented with the BigDecimal constructor, with the following result:

BigDecimal db1 = new BigDecimal("1.325"); -> 1.325
BigDecimal db2 = new BigDecimal(1.325d); -> 1.3249999999999999555910790149937383830547332763671875

Theory: From the above, it seems that JSF was taking my BigDecimal value converting it to String, than to Double, than calling a ‘new BigDecimal(double)’ on the value to get the BigDecimal – which returns the wrong value.


Fix: One way to resolve this is to use the following code:

@ManagedBean("webUtilMb")
public class WebUtilManagedBean{
  public Double roundUp(Double dvalue, int scale){
    return this.roundUp(**new BigDecimal(dvalue.toString())**, BigDecimal.ROUND_HALF_UP);
  }
  public BigDecimal roundUp(BigDecimal dbvalue, int scale){
    return dbvalue.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
  }
}

But that just seems like a hack to me.

Any ideas on fixing this and the reasons behind it. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:17:01+00:00Added an answer on June 18, 2026 at 6:17 am

    Ideally this would be done via f:convertNumber if it had a ’rounding’ attribute, but it doesn’t.

    The clean way to do this would be to write your own roundup f:converter class, and use an f:converter tag where appropriate in the XHTML instead of using the EL method call. Your converter will get the value as an Object, i.e. a BigDecimal, and is responsible for converting it to a String itself. See the documentation for f:converter. Surprised that JSF doesn’t do this correctly but I guess once you get into EL method calls everything is a String. Something like this (warning: untested):

    import java.math.BigDecimal;
    import javax.faces.component.UIComponent;
    import javax.faces.context.FacesContext;
    import javax.faces.convert.Converter;
    import javax.faces.convert.FacesConverter;
    
    /**
     * Use via e.g.:
     * <pre>
     * &lt;h:outputText value="#{EL}"&gt;
     *  &lt;f:converter id="com.edsi.jsf.RoundHalfUp"/&gt;
     *  &lt;f:attribute name="decimalPlaces" value="2"/&gt;
     * &lt;/h:outputText&gt;
     * <pre>
     * @author Esmond Pitt
     */
    @FacesConverter(value="com.edsi.jsf.RoundHalfUp")
    public class RoundHalfUpConverter implements Converter
    {
    
        @Override
        public Object getAsObject(FacesContext context, UIComponent component, String value)
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        @Override
        public String getAsString(FacesContext context, UIComponent component, Object value)
        {
            BigDecimal  bd = (BigDecimal)value;
            int decimalPlaces;
            try
            {
                decimalPlaces = Integer.parseInt((String)component.getAttributes().get("decimalPlaces"));
            }
            catch (Exception exc)
            {
                decimalPlaces = 2;  // or whatever
            }
            return bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP).toString();
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be an easy problem but... I need to format a currency for
I'm trying to format a BigDecimal value by using methods of DecimalFormat.format(). My problem,
I faced a problem - I need to use a macro value both as
I need to take data from external file and format it, the problem is,
I have a problem I need to convert char [] to String, hexadecimal format
i've a little json encode problem : i need to encode an object format
I have a properties file in this format attribute1=ó The problem is I need
i need to format date like 2010-04-21 11:35:22.440. can anyone help me? the problem
I create the function to convert the Currency to Word Format but my problem
I can upload a picture of the problem if you need one, but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.