My question is where exactly and hoow to use decimal format to have numbers prin
ID: 3766045 • Letter: M
Question
My question is where exactly and hoow to use decimal format to have numbers printed in ##.## format only?
Thanks!! HERE IS THE CLASS
mport java.text.DecimalFormat;
import java.util.Scanner;
public class PenguinShavedIce {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("Enter the name of the shop");
String Name = scan.nextLine();
System.out.print("Enter zip code: ");
String Location = scan.nextLine();
IceStand iceStand = new IceStand(Name, Location);
System.out.println();
int small = 0;
while(small != -1)
{
System.out.print("Enter number of small (-1 to stop).....");
small = scan.nextInt();
System.out.print("Enter number of medium......");
int medium = scan.nextInt();
System.out.print("Enter number of large......");
int large = scan.nextInt();
DecimalFormat fmt = new DecimalFormat("##.##");
System.out.println(iceStand.ToString());
System.out.println(" ////////~~~\\\\\\\\");
Snowball smallSnowball = new Snowball();
Snowball mediumSnowball = new Snowball();
Snowball largeSnowball = new Snowball();
double smallTotal = smallSnowball.calculatePrice("Small",small);
double mediumTotal = mediumSnowball.calculatePrice("Medium", medium);
double largeTotal = largeSnowball.calculatePrice("Large",large);
System.out.println(smallSnowball.ToString());
System.out.println(mediumSnowball.ToString());
System.out.println(largeSnowball.ToString());
double subTotal = (smallTotal+ mediumTotal+ largeTotal);
System.out.println("");
System.out.println("Subtotal "+ "$"+fmt.format(subTotal));
double TAX =Snowball.calculateTAX(subTotal);
System.out.println("Tax(6%) " +" $"+Snowball.calculateTAX( subTotal));
System.out.println("Total bill " + "$"+(subTotal+TAX));
}
}
}
Explanation / Answer
/*
* A DecimalFormat class is usually used while displaying results of a calculations
* A double (or float) returns 10places after decimal point, but we are only interested in 2-3 places
* Hence we need to user DecimalFormat class for rounding and displaying only the required number of digits
* Its constructor DecimalFormat fmt = new DecimalFormat("#.00");
* means print all the numbers before decimal and only two digits after decimal
*
* //example
double value = 4535.246456;
System.out.println("Original double value is: "+value);
DecimalFormat fmt = new DecimalFormat("#.00");
System.out.println("double after format: "+fmt.format(value));
*/
import java.text.DecimalFormat;
import java.util.Scanner;
public class PenguinShavedIce {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the name of the shop");
String Name = scan.nextLine();
System.out.print("Enter zip code: ");
String Location = scan.nextLine();
IceStand iceStand = new IceStand(Name, Location);
System.out.println();
int small = 0;
while (small != -1) {
System.out.print("Enter number of small (-1 to stop).....");
small = scan.nextInt();
System.out.print("Enter number of medium......");
int medium = scan.nextInt();
System.out.print("Enter number of large......");
int large = scan.nextInt();
DecimalFormat fmt = new DecimalFormat("#.00");
System.out.println(iceStand.ToString());
System.out.println(" ////////~~~\\\\\\\\");
Snowball smallSnowball = new Snowball();
Snowball mediumSnowball = new Snowball();
Snowball largeSnowball = new Snowball();
double smallTotal = smallSnowball.calculatePrice("Small", small);
double mediumTotal = mediumSnowball.calculatePrice("Medium", medium);
double largeTotal = largeSnowball.calculatePrice("Large", large);
System.out.println(smallSnowball.ToString());
System.out.println(mediumSnowball.ToString());
System.out.println(largeSnowball.ToString());
double subTotal = (smallTotal + mediumTotal + largeTotal);
System.out.println("");
// here
System.out.println("Subtotal " + "$" + fmt.format(subTotal));
double TAX = Snowball.calculateTAX(subTotal);
// here
System.out.println("Tax(6%) " + " $" + Snowball.calculateTAX(subTotal));
// and here
System.out.println("Total bill " + "$" + (subTotal + TAX));
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.