0

final double YearDep= 0.15; //this is for each year a car loses of the value before

     double HybridDepreciation= HybridCarCost * Math.pow(1-YearDep,5);
     double HybridFuelCost=(Miles*5*GasCost)/HybridCarMPG;

     double GasPowerDepreciation=GasPowerCost * Math.pow(1-YearDep,5);
     double GasPowerFuelCost=(Miles *5*GasCost)/GasPowerMPG;

     double HybridTotalCost=HybridCarCost - HybridDepreciation + HybridFuelCost;
     double GasPowerTotalCost= GasPowerCost - GasPowerDepreciation + GasPowerFuelCost;

     System.out.printf("The total cost for the " + HybridName + " " + "is" +" $"+ HybridTotalCost);

     System.out.println();
     System.out.printf("The total cost for the " + GasPowerName + " "+ "is" + " $"+ GasPowerTotalCost);
jerry lin
  • 11
  • 4

4 Answers4

1

use DecimalFormat for this:

DecimalFormat df = new DecimalFormat(".00");
System.out.printf("The total cost for the " + HybridName + " " + "is" +" $"+ df.format(HybridTotalCost));
 System.out.println();
 System.out.printf("The total cost for the " + GasPowerName + " "+ "is" + " $"+ df.format(GasPowerTotalCost));

Or you can use

String.format("The total cost for the  %s is $%.2f", HybridName ,HybridTotalCost)
Jens
  • 67,715
  • 15
  • 98
  • 113
0
 double roundOff = Math.round(a*100)/100.0;

or

 DecimalFormat number = new DecimalFormat("#.00");
 number.format(new Double(2223.8990))
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
0
DecimalFormat df = new DecimalFormat("#.00"); 

then

df.format(<your_value>);

Or

System.out.println( String.format( "%.2f", <your_value> ) );
shikjohari
  • 2,278
  • 11
  • 23
0

You can use.

double roundOff1 = (double) Math.round(HybridTotalCost);
double roundOff2 = (double) Math.round(GasPowerTotalCost);

and you can also use.

DecimalFormat df = new DecimalFormat("###.##");
System.out.printf("The total cost for the " + HybridName + " " + "is" +" $"+ df.format(HybridTotalCost));
System.out.println();
System.out.printf("The total cost for the " + GasPowerName + " "+ "is" + " $"+df.format(GasPowerTotalCost));
praveen_programmer
  • 1,072
  • 11
  • 25