簡體   English   中英

我不確定如何在我的Java代碼中正確地舍入它

[英]I'm not sure how to round this properly in my Java code

我是Java的新手,我最近編寫了一個代碼,用於計算為xy物品支付的x金額需要多少變化。 效果很好; 我唯一的問題是,只要在百分之一的地方沒有任何變更(例如:4.60美元),它就會減少到十分位數(4.6美元)。

如果有人知道如何解決這個問題,我將非常感激。 我在下面發布了代碼。

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

    }
}

編輯:謝謝大家回答我的問題! 我最終使用DecimalFormat來解決它,現在它運行得很好。

此行為是預期的。 您不希望數字帶有尾隨零。 您可以使用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

編輯:

您甚至可以通過將RoundingMode設置為df.setRoundingMode(RoundingMode.UP);來告訴DecimalFormat如何RoundingMode您的數字df.setRoundingMode(RoundingMode.UP);

你可以這樣打電話:

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

所以在你的情況下:

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

無論何時想要將其舍入到某些有效數字, String.format()都很有用。 在這種情況下,“f”代表浮動。

String.format()方法是我個人的偏好。 例如:

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

%.2f會將任何浮點數('f'代表浮點數)舍入到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...

如果您開始使用Java,可能需要讀取String.format() 這是一種非常強大而有用的方法。

據我所知:

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

為您未來的項目提供最好的服務!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM