String.format() - . :
float z;
System.out.println(String.format("Change Owed: $%.2f", (float) ((z) / 100)));
%. 2f float ('f' float) 2 , 'f', , . :
System.out.println(String.format("Change Owed: $%.3f", (float) ((z) / 100)));
System.out.println(String.format("Change Owed: $%.4f", (float) ((z) / 100)));
, String.format(), Java. .
, :
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
double x;
double y;
double z;
System.out.print("Enter the price of the product: $");
x = scan.nextDouble();
System.out.print("Enter what you payed with: $");
y = scan.nextDouble();
z = (int) Math.round(100 * (y - x));
System.out.println(String.format("Change Owed: $%.2f", (float) ((z) / 100)));
int q = (int) (z / 25);
int d = (int) ((z % 25 / 10));
int n = (int) ((z % 25 % 10 / 5));
int p = (int) (z % 25 % 10 % 5);
System.out.println("Quarters: " + q);
System.out.println("Dimes: " + d);
System.out.println("Nickels: " + n);
System.out.println("Pennies: " + p);
}
!
source
share