Rounding doubles

K

KyoGaSuki

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.
 
P

Peter Duniho

[...]
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
 
K

KyoGaSuki

[...]
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
 
P

Peter Duniho

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
 
A

Arne Vajhøj

KyoGaSuki said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top