Help with rounding decimals
I need some help! I can't seem to get my output to round to two decimal places. Can anyone give me a hand with this I have tried countless ways of doing this and none of them have worked. I thought the code - DecimalFormat fmt = new DecimalFormat ("0.##"); would work but it hasn't.
Here is my program-
import java.awt.Color;
import java.awt.Dimension;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class CarTracker {
//This declares Variables
private String firstName, lastName;
private double fares;
private double fuelUsed, distanceDriven;
public CarTracker(String first, String last) {
firstName = first;
lastName = last;
fares = 0.0;
fuelUsed = 0.0;
distanceDriven = 0.0;
}
//This sets up all the variables that we will be using and uses them as a string or as a double varaible.
public String driversName() {
return (firstName + ' ' + lastName);
}
public void addFare(double fare) {
fares += Math.abs(fare);
}
public void addFuel(double fuel) {
fuelUsed += Math.abs(fuel);
}
public void drive(double distance) {
distanceDriven += Math.abs(distance);
}
public double readMeter() {
return fares;
}
public double readFuelGage() {
return fuelUsed;
}
public double readOdometer() {
return distanceDriven;
}
public double fuelEfficency() {
return (fuelUsed / distanceDriven);
}
public double fareRate() {
return (fares / distanceDriven);
}
public double mileageCost(double fuelPurchased) {
return (fuelPurchased / fuelUsed);
}
public double Profit() {
return (fares - fuelUsed);
}
public static void main(String[ ] args ) {
Scanner reader;
reader = new Scanner( System.in );
//This is where the program will ask the user to enter values for the questions posted.
System.out.println("Enter the driver's first name:");
String firstName = reader.nextLine();
System.out.println("Enter the driver's last name:");
String lastName = reader.nextLine();
CarTracker driver = new CarTracker(firstName, lastName);
System.out.println("Enter the amount paid to the driver:");
double fares = reader.nextDouble();
driver.addFare(fares);
System.out.println("Enter the amount the driver paid for gas:");
double gasPrice = reader.nextDouble();
System.out.println("Enter the distance the driver traveled:");
double distance = reader.nextDouble();
driver.drive(distance);
System.out.println("Please enter the amount of gasoline the Driver used:");
double fuel = reader.nextDouble();
// Presents two colored panels nested within a third.
JFrame frame = new JFrame ("Nested Panels");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JPanel primary = new JPanel();
primary.setPreferredSize (new Dimension(250, 250));
primary.setBackground (Color.green);
JLabel label1 = new JLabel ("Name: " + driver.driversName());
JLabel label2 = new JLabel ("Miles Per Gallon: = " + (distance/fuel));
JLabel label3 = new JLabel ("Fares Per Mile: = " + (fares/distance));
JLabel label4 = new JLabel ("Gas Paid Per Mile: = " + (gasPrice/distance));
JLabel label5 = new JLabel ("Total Profit: = " + (fares-gasPrice));
primary.add (label1);
primary.add (label2);
primary.add (label3);
primary.add (label4);
primary.add (label5);
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
}