DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2005
    Posts
    10

    round off 2 decimal places

    Halo

    can someone tell me how to convert a double variable to 2 decimals? I try using the below code but it cannot work.

    DecimalFormat df = new DecimalFormat("0.00");

    String a = df.format(123.5%11);

    double AA = Double.parseDouble(a);



    for instance i want AA = 2.50 instead of 2.5. the program keep on giving me 2.5.

    I need the data type to be Double but not String.

    thks

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    adding zeros to the end of a double is pointless.

    2.5 = 2.50 = 2.500 = 2.500000000000

    If you want it to display the trailing zeros, it has to become a string.

    Your code here will give you "2.50", but when displaying numbers, leading and trailing zeros are removed, so it must stay as string.
    Code:
    DecimalFormat df = new DecimalFormat("0.00");
    
    String a = df.format(123.5%11);

Similar Threads

  1. Currency and forcing Decimal Places
    By Damien in forum ASP.NET
    Replies: 3
    Last Post: 08-18-2002, 04:00 PM
  2. Conversion of decimal in Oracle to Access 2000
    By Villy Olsson in forum Database
    Replies: 4
    Last Post: 04-06-2002, 08:01 PM
  3. Numeric decimal places in ADO control
    By rkbnair in forum VB Classic
    Replies: 0
    Last Post: 01-31-2002, 12:48 PM
  4. Calculated value and decimal places
    By Jim C. in forum Database
    Replies: 2
    Last Post: 01-03-2001, 08:12 AM
  5. change decimal places value
    By rh in forum VB Classic
    Replies: 3
    Last Post: 07-27-2000, 07:36 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links