This discussion is archived
1 2 Previous Next 18 Replies Latest reply: Feb 1, 2007 11:45 AM by 807599 RSS

Help Round decimal 2 palces

807599 Newbie
Currently Being Moderated
I have searched, but I cannot seem to find an answer to my question.

I am a student in into to programing and I am trying to figure out why my decimals will not round. I have tried everything I can find, but being new to java programing, I am sure I am making a syntatical error somewhere. My program works other than not displaying the decimal places the way I want, 200.00 not 200.0.

The following is my code, I have commented out each of the decimal formats I have tried.

import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class CheckingAcct
{
     
     //int decimalPlaces = 2;

// Truncates the big decimal value.
//bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_DOWN);
     //String string = bd.toString();
          //DecimalFormat df = new DecimalFormat ( "0.0#" );
               //DecimalFormat twoDigits = new DecimalFormat ( "0.0#" );
          //DecimalFormat decimalFormat = (DecimalFormat)numberFormat;
          //decimalFormat.applyPattern("0.0#");

     public static void main (String args[])
     {
          
          String strBalance,
               strDeposit,
               strChecks;
                              
          double balance = 200.00,
          deposit,
               lbcharge = 2.00,
               nsfcharge = 25.00,
               checks;
               
          //Display Starting Balance
          
          JOptionPane.showMessageDialog (null, "Your current balance is:$" + balance);
          
          //calculate balance
          
     strDeposit = JOptionPane.showInputDialog ("Please enter your deposit amount:$");
     deposit = Double.parseDouble (strDeposit);
     balance = deposit + balance;
     JOptionPane.showMessageDialog (null, "Your current balance after deposit is:$" + balance);
     
     strChecks = JOptionPane.showInputDialog ("Please enter the total amount of checks written:$");
     checks = Double.parseDouble (strChecks);
     balance = balance - checks;
     
     //Low Balance
     if (balance < 100.00)
          {
          JOptionPane.showMessageDialog (null, "You have fallen below the minium required balance and you have been charged:$" + lbcharge);     
               balance = balance - lbcharge;
          }
          else
          {
               lbcharge = 0.00;
          }
          //NSF Charge
     if (balance <= 0.00)
          {
          JOptionPane.showMessageDialog (null, "You have a negative balance and you have been charged:$" + nsfcharge);     
               balance = (balance - nsfcharge);
          JOptionPane.showMessageDialog (null, "Your current balance is:$" + balance);     
          }
          else
          JOptionPane.showMessageDialog (null, "Your balance is:$" + balance);
     
     System.exit (0);
     }
}
  • 1. Re: Help Round decimal 2 palces
    807599 Newbie
    Currently Being Moderated
    Go back and read the API. You are on the right track with:

    DecimalFormat df = new DecimalFormat ( "0.0#" );


    But not quite there.
  • 2. Re: Help Round decimal 2 palces
    807599 Newbie
    Currently Being Moderated
    Thank you for the reply, could you possibly nudge me in the right direction, being new to this, I really do not wholely(sp?), understand what I am looking at? I have changed the format to:

    DecimalFormat df = new DecimalFormat ( "#.##" );
  • 3. Re: Help Round decimal 2 palces
    807599 Newbie
    Currently Being Moderated
    Thank you for the reply, could you possibly nudge me
    in the right direction, being new to this, I really
    do not wholely(sp?), understand what I am looking at?
    I have changed the format to:

    DecimalFormat df = new DecimalFormat ( "#.##" );
    Are you reading the API?

    I nudged you to the API where it is quite clear. Anymore help would be spoonfeeding, which really isn't helpful to either of us.
  • 4. Re: Help Round decimal 2 palces
    807599 Newbie
    Currently Being Moderated
    I tend to use super class NumberFormat, because I can never remember the jiggery-pokery of those patterns:
    import java.text.*;
    
    public class Example {
        public static void main(String[] args) {
            NumberFormat fmt = NumberFormat.getInstance();
            fmt.setMinimumFractionDigits(2);
            fmt.setMaximumFractionDigits(2);
            System.out.println(fmt.format(200.0));
            System.out.println(fmt.format(200.115));
            System.out.println(fmt.format(200.125));
        }
    }
    Just be aware that NumberFormat/DecimalFormat uses half-even rounding.
  • 5. Re: Help Round decimal 2 palces
    807599 Newbie
    Currently Being Moderated
    Thank you for the reply, could you possibly nudge
    me
    in the right direction, being new to this, I
    really
    do not wholely(sp?), understand what I am looking
    at?
    I have changed the format to:

    DecimalFormat df = new DecimalFormat ( "#.##" );
    Are you reading the API?

    I nudged you to the API where it is quite clear.
    Anymore help would be spoonfeeding, which really
    isn't helpful to either of us.
    What I was getting at is where can I find the api?
  • 6. Re: Help Round decimal 2 palces
    807599 Newbie
    Currently Being Moderated
    http://java.sun.com/reference/api/
  • 7. Re: Help Round decimal 2 palces
    807599 Newbie
    Currently Being Moderated
    I tend to use super class NumberFormat, because I can
    never remember the jiggery-pokery of those patterns:
    import java.text.*;
    
    public class Example {
    public static void main(String[] args) {
    NumberFormat fmt = NumberFormat.getInstance();
    fmt.setMinimumFractionDigits(2);
    fmt.setMaximumFractionDigits(2);
    System.out.println(fmt.format(200.0));
    System.out.println(fmt.format(200.115));
    System.out.println(fmt.format(200.125));
    }
    }
    Just be aware that NumberFormat/DecimalFormat uses
    half-even rounding.
    This confirms another thought I had, that I must format each number I want to use. Thank you.
  • 8. Re: Help Round decimal 2 palces
    807599 Newbie
    Currently Being Moderated
    I Was not asking to be spoon feed zadok, I have had 3 classes and I am just learning.


    DrLaszloJamf ,

    I have read through this, http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html

    As I am reading this, if I do not set the local it will apply the default, thus meaning I will not have to use the getInstance(loc); portion of the code?
  • 9. Re: Help Round decimal 2 palces
    807599 Newbie
    Currently Being Moderated
    Yes, you can omit the locale.
  • 10. Re: Help Round decimal 2 palces
    807599 Newbie
    Currently Being Moderated
    DecimalFormat df = new DecimalFormat("0.00");
    will give you two decimal places
  • 11. Re: Help Round decimal 2 palces
    807599 Newbie
    Currently Being Moderated
    I used that before, but it is not giving me the decimals.

    I am reading the api document, though I do not fully understand what I am reading. I see from DrLaszloJamf post, that I need to format each number, but I have not yet figured that out completely.

    Message was edited by:
    829
  • 12. Re: Help Round decimal 2 palces
    807599 Newbie
    Currently Being Moderated
    It's not clear what you mean by "format each number"?
    Do you mean writing
    System.out.print(fmt.format(doubleValue));
    instead of
    System.out.print(doubleValue);
  • 13. Re: Help Round decimal 2 palces
    807599 Newbie
    Currently Being Moderated
    I used that before, but it is not giving me the decimals.
    Are you sure? Try
    package testing;
    
    import java.text.DecimalFormat;
    
    public class Test
    {
      public static void main(String[] args)
      {
        DecimalFormat df = new DecimalFormat("0.00");
        double d = 567.1289756537;
        System.out.println(df.format(d));
      }
    }
    Jukka
  • 14. Re: Help Round decimal 2 palces
    807599 Newbie
    Currently Being Moderated
    It's not clear what you mean by "format each
    number"?
    Do you mean writing
    System.out.print(fmt.format(doubleValue));
    instead of
    System.out.print(doubleValue);
    I really not sure what I am getting at, other than each number I hardcode must have some type of print function. With only 3 classes under my belt, I may just be chewing off more than I can digest at this point. I am just trying to get ahead and I am sure we will go over this at some point this semester.
1 2 Previous Next