Thank you All for your help!!

but i have noticed that if you pass double value 45.999 it rounds off the
fractional part and returns 46.00
Is it a bug or NumberFormat class behaves like this only ?

below is the code to do it.

import java.text.*;
public class Truncate
{
  public static String trim(double d)
  {
    DecimalFormat format = new DecimalFormat();
    format.applyPattern("0.00");
//  format.setMaximumFractionDigits(2);
    return format.format(d);
  }
  public static void main(String[] args)
  {
    Truncate t = new Truncate();
    System.out.println(t.trim(234.8987));
  }
}

-----Original Message-----
From: Chris Pratt [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, August 29, 2001 2:46 AM
To: [EMAIL PROTECTED]
Subject: Re: offtopic : to truncate a double


Take a look at the java.text.DecimalFormat()
    (*Chris*)

----- Original Message -----
From: "Richard Yee" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, August 28, 2001 1:09 PM
Subject: Re: [JSP-INTEREST] offtopic : to truncate a double


> Raj,
> There is not a method that truncates a double to a certain
> precision.  Check out this link for an explanation on Performing Exact
> Calculations With Floating-Point Numbers
>
> http://developer.java.sun.com/developer/JDCTechTips/2001/tt0807.html
>
> Basically, can either use the BigDecimal class that supports arbitrary
> precision numbersinstead of the double type or you can write a method that
> rounds a number to the desired precision for you.
>
> ie
> /**
>   * Trim a double value to two decimal points
>   *
>    public static double trim(double d)
>    {
>      BigDecimal bd = new BigDecimal(d);
>      return bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
>    }
>
>
> At 09:53 PM 8/27/01 -0700, you wrote:
> >Hi all,
> >
> >i have variable double x = 34.8976 ;
> >i want only 2 digits after decimal. Is there any method  returning double
> >and gives value x = 34.89 ???
> >
> >Cheers!!
> >Raj
> >
>
>===========================================================================
> >To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> >JSP-INTEREST".
> >For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
> >Some relevant FAQs on JSP/Servlets can be found at:
> >
> >  http://java.sun.com/products/jsp/faq.html
> >  http://www.esperanto.org.nz/jsp/jspfaq.html
> >  http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> >  http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
>
>
===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
> For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.html
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to