简体   繁体   中英

Im having trouble with formatting my output of in my program

The code is not formatting right

At first the code would spit out 1.0 when I wanted 1.00. So, I used an if statement to fix that. Then the code would give .5 not .50, So I tried the same solution but it did not work. I was wondering if I could make a formatter that would add an extra 0 if the there was only one number to the right of the decimal to fix my problem.

    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 instead of .50

In your particular case (your method is returning string), one way you can do this is by utilizing the String#format() method as @Taslim has touched on, it would go something like this:

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

Yet another way is by utilizing the DecimalFormat#format() method as already mentioned by @Jens, it would go something like this:

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

The DecimalFormatSymbols.getInstance(Locale.US) you see within the DecimalFormat variable initialization is used so that your numerical value is presented in your specific country's numerical format.

And yet another, more long winded way to do this for smaller values would be to utilize both the Math#pow() and Math#round() methods like this:

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

For larger, more complex values however you may want to perhaps do it by utilizing BigDecimal#setScale() method which provides far more accuracy when doing calculations with Float or Double data type values ( which are inaccurate )....something like this:

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

}

Take your pick ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM