CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2011
    Posts
    3

    In jsp page After decimal i need only 2 values(for ex:477.74)

    Hi Guys,
    I displaying report for work wise progress in jsp. At end of line i calculating total amount of Contract . In my result it is displaying 477.74999999994. For reference you can see attached screen shot file. But i want result in this format 477.74(i dont want to be round off).
    I attached jsp page for your reference .
    Please help me & need code for that
    Attached Files Attached Files

  2. #2
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: In jsp page After decimal i need only 2 values(for ex:477.74)

    Hi,

    I suppose the amount is stored in a BigDecimal type.

    Create an utility Java Class a static method like this:
    Code:
    public class MyUtils{
       public static String truncateBigDecimal(BigDecimal bg, Locale loc , int decimals) {
          bg = bg.setScale(decimals, RoundingMode.DOWN); 
          DecimalFormat df = new DecimalFormat();
          if (loc != null) 
             df.setDecimalFormatSymbols(new DecimalFormatSymbols(loc));
          return df.format(bg);
       }
    }
    From your JSP, first import this util class, and then print amount with appropriate format:
    Code:
    <&#37;@ page import="<package>.MyUtils" %>
    <!-- [...] --> 
    
    <define id="amount" name="YourFormClass" property="amount" type="java.math.BigDecimal" />
    <%=MyUtils.truncateBigDecimal(amount, Locale.US, 2)%>
    Hope this helps. Regards,

    Albert.
    Last edited by AlbertGM; August 23rd, 2011 at 06:05 AM.
    Please, correct me. I'm just learning.... and sorry for my english :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured