Rounding doubles

Discussion in 'Java' started by KyoGaSuki, Feb 12, 2008.

  1. KyoGaSuki

    KyoGaSuki Guest

    We were given a project to do in our Beginner Java class. We are to
    calculate different aspects of a water bill (such as water usage cost
    and sewer usage costs) and I have managed to get it working, except I
    am suppose to round the numbers in the results, and I don't know how
    to do that with doubles. Here are the directions of the assignment
    and then what i have so far.:

    The user will enter the number of gallons of water they used during
    the billing period (whole number). after this has been entered, the
    following must be displayed, all as currency:
    Base charge: $15.79
    Water usage: $0.27 per 100 gallons
    Sewer usage: $1.49 per 1000 gallons
    Subtotal
    Tax: 6% of subtotal
    Total

    my code so far:


    /**
    * @(#)try1.java
    *
    * try1 application
    *
    * @author
    * @version 1.00 2008/2/11
    */

    import java.util.*;
    import javax.swing.*;
    public class try1 {

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.print("Please enter the amount of gallons used: ");
    int gal = input.nextInt();
    int galhund = gal/100; //amount rounded to the hundreds
    int galthous = gal/1000; //amount rounded to the thousands
    double base = 15.79; //base service charge
    double water = galhund*0.27; //water usage charge
    double sewer = galthous*1.49; //sewer usage charge
    double sub = base+water+sewer; //subtotal
    double tax = sub*.06; //subtotal times 6% tax
    double total = sub+tax; //total due
    System.out.println("Base Service $" + base);
    System.out.println("Water Usage $" + water);
    System.out.println("Sewer Usage $" + sewer);
    System.out.println("Subtotal $" + sub);
    System.out.println("Tax $" + tax);
    System.out.println("Total Due $" + total);



    }
    }


    Currently everything seems to calculate correctly, except it comes out
    looking like this:

    Please enter the amount of gallons used: 12345
    Base Service $15.79
    Water Usage $33.21
    Sewer Usage $17.88
    Subtotal $66.88
    Tax $4.0127999999999995 ( <-- need to round to 2
    decimals )
    Total Due $70.8928 ( <-- need to round to 2 decimals )

    Process completed.
     
    KyoGaSuki, Feb 12, 2008
    #1
    1. Advertising

  2. KyoGaSuki

    ralpe Guest

    Check out the NumberFormat class.
     
    ralpe, Feb 12, 2008
    #2
    1. Advertising

  3. KyoGaSuki

    Peter Duniho Guest

    On Mon, 11 Feb 2008 21:41:55 -0800, KyoGaSuki
    <> wrote:

    > [...]
    > Currently everything seems to calculate correctly, except it comes out
    > looking like this:
    >
    > Please enter the amount of gallons used: 12345
    > Base Service $15.79
    > Water Usage $33.21
    > Sewer Usage $17.88
    > Subtotal $66.88
    > Tax $4.0127999999999995 ( <-- need to round to 2
    > decimals )
    > Total Due $70.8928 ( <-- need to round to 2 decimals )


    While the NumberFormat (or more specifically in this case, DecimalFormat)
    class could be applied here, it seems to me that you may find it simpler
    to just use the printf() method instead of println(). With printf() you
    can provide a formatting string. For example:

    System.out.printf("Total Due $%.2f%n", total);

    Which tells the printf() method to format the value in "total" as a
    floating point numeric value (the "f") with 2 decimal places (the ".2").
    (The "%n" just says to include a newline character at the end, just as
    println() would do normally).

    You could also use the String.format() method instead, and print the
    result using println() as you're already doing. It has the same syntax
    for the format string.

    See http://java.sun.com/javase/6/docs/api/java/util/Formatter.html for
    more details on the format strings allowable.

    Pete
     
    Peter Duniho, Feb 12, 2008
    #3
  4. KyoGaSuki

    KyoGaSuki Guest

    On Feb 12, 1:08 am, "Peter Duniho" <>
    wrote:
    > On Mon, 11 Feb 2008 21:41:55 -0800, KyoGaSuki
    >
    > <> wrote:
    > > [...]
    > > Currently everything seems to calculate correctly, except it comes out
    > > looking like this:

    >
    > > Please enter the amount of gallons used: 12345
    > > Base Service $15.79
    > > Water Usage $33.21
    > > Sewer Usage $17.88
    > > Subtotal $66.88
    > > Tax $4.0127999999999995 ( <-- need to round to 2
    > > decimals )
    > > Total Due $70.8928 ( <-- need to round to 2 decimals )

    >
    > While the NumberFormat (or more specifically in this case, DecimalFormat)
    > class could be applied here, it seems to me that you may find it simpler
    > to just use the printf() method instead of println(). With printf() you
    > can provide a formatting string. For example:
    >
    > System.out.printf("Total Due $%.2f%n", total);
    >
    > Which tells the printf() method to format the value in "total" as a
    > floating point numeric value (the "f") with 2 decimal places (the ".2").
    > (The "%n" just says to include a newline character at the end, just as
    > println() would do normally).
    >
    > You could also use the String.format() method instead, and print the
    > result using println() as you're already doing. It has the same syntax
    > for the format string.
    >
    > Seehttp://java.sun.com/javase/6/docs/api/java/util/Formatter.htmlfor
    > more details on the format strings allowable.
    >
    > Pete


    Thank you both SO much! The only question I have is that in the tax
    part, how can I get it to display a space if it isn't a double digit?

    for example:
    $ 4.01 instead of $4.01
     
    KyoGaSuki, Feb 12, 2008
    #4
  5. KyoGaSuki

    Peter Duniho Guest

    On Mon, 11 Feb 2008 22:50:42 -0800, KyoGaSuki
    <> wrote:

    > Thank you both SO much! The only question I have is that in the tax
    > part, how can I get it to display a space if it isn't a double digit?


    See the page to which I referred you. You should specifically look for
    the discussion on the "width" part of a format specifier.

    Pete
     
    Peter Duniho, Feb 12, 2008
    #5
  6. KyoGaSuki

    Roedy Green Guest

    Roedy Green, Feb 12, 2008
    #6
  7. KyoGaSuki

    Arne Vajhøj Guest

    KyoGaSuki wrote:
    > We were given a project to do in our Beginner Java class. We are to
    > calculate different aspects of a water bill (such as water usage cost
    > and sewer usage costs) and I have managed to get it working, except I
    > am suppose to round the numbers in the results, and I don't know how
    > to do that with doubles. Here are the directions of the assignment
    > and then what i have so far.:
    >
    > The user will enter the number of gallons of water they used during
    > the billing period (whole number). after this has been entered, the
    > following must be displayed, all as currency:
    > Base charge: $15.79
    > Water usage: $0.27 per 100 gallons
    > Sewer usage: $1.49 per 1000 gallons
    > Subtotal
    > Tax: 6% of subtotal
    > Total


    > Scanner input = new Scanner(System.in);
    > System.out.print("Please enter the amount of gallons used: ");
    > int gal = input.nextInt();
    > int galhund = gal/100; //amount rounded to the hundreds
    > int galthous = gal/1000; //amount rounded to the thousands
    > double base = 15.79; //base service charge
    > double water = galhund*0.27; //water usage charge
    > double sewer = galthous*1.49; //sewer usage charge
    > double sub = base+water+sewer; //subtotal
    > double tax = sub*.06; //subtotal times 6% tax
    > double total = sub+tax; //total due
    > System.out.println("Base Service $" + base);
    > System.out.println("Water Usage $" + water);
    > System.out.println("Sewer Usage $" + sewer);
    > System.out.println("Subtotal $" + sub);
    > System.out.println("Tax $" + tax);
    > System.out.println("Total Due $" + total);


    > Please enter the amount of gallons used: 12345
    > Base Service $15.79
    > Water Usage $33.21
    > Sewer Usage $17.88
    > Subtotal $66.88
    > Tax $4.0127999999999995 ( <-- need to round to 2
    > decimals )
    > Total Due $70.8928 ( <-- need to round to 2 decimals )


    Several formatting solutions has already been posted.

    And given that it is a beginner class then you should
    use one of them.

    But I will like to point out another solution:

    Scanner input = new Scanner(System.in);
    System.out.print("Please enter the amount of gallons used: ");
    int gal = input.nextInt();
    int galhund = gal/100;
    int galthous = gal/1000;
    BigDecimal base = new BigDecimal("15.79");
    BigDecimal water = new BigDecimal("0.27").multiply(new
    BigDecimal(galhund));
    BigDecimal sewer = new BigDecimal("1.49").multiply(new
    BigDecimal(galthous));
    BigDecimal sub = base.add(water).add(sewer);
    BigDecimal tax = sub.multiply(new
    BigDecimal("0.06")).setScale(2, RoundingMode.HALF_EVEN); // US rounding
    - use HALF_UP for european
    BigDecimal total = sub.add(tax);
    System.out.println("Base Service $" + base);
    System.out.println("Water Usage $" + water);
    System.out.println("Sewer Usage $" + sewer);
    System.out.println("Subtotal $" + sub);
    System.out.println("Tax $" + tax);
    System.out.println("Total Due $" + total);

    Arne
     
    Arne Vajhøj, Feb 13, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kloofy
    Replies:
    0
    Views:
    329
    Kloofy
    Jul 8, 2005
  2. Rick
    Replies:
    3
    Views:
    1,139
    Chris Smith
    Oct 8, 2003
  3. dan
    Replies:
    1
    Views:
    2,362
    Jack Klein
    Nov 26, 2003
  4. itportal

    Rounding floats/doubles

    itportal, Jan 16, 2006, in forum: C Programming
    Replies:
    9
    Views:
    526
    Gordon Burditt
    Jan 17, 2006
  5. wombat

    doubles/printf & rounding

    wombat, Sep 5, 2007, in forum: C++
    Replies:
    2
    Views:
    451
    James Kanze
    Sep 5, 2007
Loading...

Share This Page