簡體   English   中英

我在我的程序中格式化我的輸出時遇到問題

[英]Im having trouble with formatting my output of in my program

代碼格式不正確

起初,當我想要 1.00 時,代碼會吐出 1.0。 所以,我使用了一個 if 語句來解決這個問題。 然后代碼會給出 0.5 而不是 0.50,所以我嘗試了相同的解決方案,但沒有奏效。 我想知道我是否可以制作一個格式化程序,如果小數點右側只有一個數字來解決我的問題,它會添加一個額外的 0。

    import java.util.Scanner;

public class addCoins {
    public static void main(String[] args) {
        int quarters, dimes, nickels, pennies;

        Scanner input = new Scanner(System.in);

        System.out.println("Enter in your total coins: ");

        System.out.println();

        System.out.print("Quarters: ");
        quarters = input.nextInt();
        System.out.print("Dimes: ");
        dimes = input.nextInt();
        System.out.print("Nickels: ");
        nickels = input.nextInt();
        System.out.print("Pennies: ");
        pennies = input.nextInt();

        System.out.println();

        System.out.println(dollarAmount(quarters, dimes, nickels, pennies));
    }

    public static String dollarAmount(int quarters2, int dimes2, int nickels2, int pennies2) {

        double total = (quarters2 * .25) + (dimes2 * .10) + (nickels2 * .05) + (pennies2 * .01);

        if (total % 1.0 == 0 || total % .10 == .0) {
            return "Total: $" + total + "0";
        } else {
            return "Total: $" + total;
        }
    }

}

.5 而不是 .50

在您的特定情況下(您的方法返回字符串),您可以這樣做的一種方法是使用 @Taslim 提到的String#format()方法,它會是這樣的:

public static String dollarAmount(int quarters2, int dimes2, int nickels2, int pennies2) {
    double total = (quarters2 * .25) + (dimes2 * .10) + (nickels2 * .05) + (pennies2 * .01);
    return String.format("Total: $%.2f", total);
}

另一種方法是利用@Jens 已經提到的DecimalFormat#format()方法,它會是這樣的:

public static String dollarAmount(int quarters2, int dimes2, int nickels2, int pennies2) {
    double total = (quarters2 * .25) + (dimes2 * .10) + (nickels2 * .05) + (pennies2 * .01);
    DecimalFormat df = new DecimalFormat(".##", DecimalFormatSymbols.getInstance(Locale.US));
    return "Total: $" + df.format(total);
}

您在 DecimalFormat 變量初始化中看到的DecimalFormatSymbols.getInstance(Locale.US)被使用,以便您的數值以您的特定國家/地區的數字格式顯示。

對於較小的值,另一種更冗長的方法是同時使用Math#pow()Math#round()方法,如下所示:

public static String dollarAmount(int quarters2, int dimes2, int nickels2, int pennies2) {
    double total = (quarters2 * .25) + (dimes2 * .10) + (nickels2 * .05) + (pennies2 * .01);

    long factor = (long) Math.pow(10, 2); // 2 is the decimal places
    total = total * factor;
    long tmp = Math.round(total);
    return "Total: $" + String.valueOf((double) tmp / factor);
}

但是,對於更大、更復雜的值,您可能希望通過使用BigDecimal#setScale()方法來實現,該方法使用 Float 或 Double 數據類型值(不准確進行計算時提供更高的准確性......像這樣:

public static String dollarAmount(int quarters2, int dimes2, int nickels2, int pennies2) {
    double total = (quarters2 * .25) + (dimes2 * .10) + (nickels2 * .05) + (pennies2 * .01);

    BigDecimal bd = new BigDecimal(total);
    // The 2 used below is the decimal places. You can
    // use whatever rounding mode you want.
    bd = bd.setScale(2, RoundingMode.HALF_UP); 
    return "Total: $" + String.valueOf(bd.doubleValue());

}

隨便選吧;)

暫無
暫無

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

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