I'm not sure how to do it right in my java code

I am new to Java and recently I wrote code that calculates how many changes you will need for x the amount of money paid at the price of y. This works well; my only problem is that whenever there are no changes in the hundredth places (for example: 4.60 US dollars), it is rounded up to tenth place (4.6 dollars).

If anyone knows how to fix this, I would be very grateful. I have the code below.

class Main {
    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.print("Change Owed: $");
      System.out.println((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);

    }
}

Edit: Thanks to everyone who answered my question! I ended up working with DecimalFormat to solve this problem and now it works great.

+4
source share
3 answers

You can call something like this:

String.format("%.2f", i);

, :

...
System.out.print("Change Owed: $");
System.out.println((String.format("%.2f", z)/100));
...

String.format() , . "f" float.

+2

. , . DecimalFormat String , .

:

DecimalFormat df = new DecimalFormat("#0.00");
double d = 4.7d;
System.out.println(df.format(d));

d = 5.678d;
System.out.println(df.format(d));

:

4.70
5.68

DecimalFormat:

DecimalFormat df = new DecimalFormat("$#0.00");

:

$4.70
$5.68

EDIT:

DecimalFormat, , RoundingMode df.setRoundingMode(RoundingMode.UP);

+2

String.format() - . :

float z;
System.out.println(String.format("Change Owed: $%.2f", (float) ((z) / 100)));

%. 2f float ('f' float) 2 , 'f', , . :

//3 decimal points
System.out.println(String.format("Change Owed: $%.3f", (float) ((z) / 100)));

//4 decimal points
System.out.println(String.format("Change Owed: $%.4f", (float) ((z) / 100)));

// and so forth...

, 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);
}

!

+1
source

Source: https://habr.com/ru/post/1612771/


All Articles