Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Any tips on printing out "coinTotal" displayed up to second decimal point?

package com.javaprojects;

// import packages
import java.util.Scanner;
import java.text.DecimalFormat;

public class Main {

    /* The program below is a coin counter that prompts a user to enter the amount of coins
    that they have. The program then stores the count in variables, produces a sum and returns
    the total in dollars.
    */

    // declare private static java.text.DecimalFormat
    private static DecimalFormat df2 = new DecimalFormat(".##");

    public static void main(String[] args) {

        // declare variables
        Scanner stdin;
        double quarters;
        double dimes;
        double nickels;
        double pennies;
        double coinTotal;

        // assign values to variables after prompting user to enter coin counts
        stdin = new Scanner (System.in);
        System.out.println("How many quarters do you have?");
        quarters = stdin.nextDouble() * 0.25;
        System.out.println("How many dimes do you have?");
        dimes = stdin.nextDouble() * 0.10;
        System.out.println("How many nickles do you have?");
        nickels = stdin.nextDouble() * 0.05;
        System.out.println("How many pennies do you have?");
        pennies = stdin.nextDouble() * 0.01;
        coinTotal = quarters + dimes + nickels + pennies;

        // print statement containing the result
        System.out.println("You have $" + coinTotal);


    } // end main
} // end class

1 Answer

Kyle Knapp
Kyle Knapp
21,526 Points

It looks like DecimalFormat df2 is defined near the top of the program but is never used. You just need to apply df2 to coinTotal.

System.out.println("You have $" + df2.format(coinTotal));

Hey Kyle, thanks for the reply. I made a few changes based on your feedback and it works now!

You are awesome.

private static DecimalFormat df2 = new DecimalFormat(".##");
// change made to display to hundredths by default
private static DecimalFormat df2 = new DecimalFormat(".00");