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