round off 2 decimal places


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: round off 2 decimal places

  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

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


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center