3

Here is my code:

System.out.printf("\n%-10s%9s%11s%13s%9s\n",
            "yearBuilt","area(sqf)","price","replaceRoof","sqfPrice");

System.out.printf("\n%-10d%9.1f$%11.2f%13s$%8.2f\n",
            house1.getYear(),house1.getSquareFeet(),house1.getPrice(),house1.isRoofChangeNeeded(),house1.calcPricePerSqf());

And here is the output I'm getting:

    yearBuilt area(sqf)      price  replaceRoof sqfPrice


    1996         2395.0$  195000.00         true$   81.42

This is the output I want:

    yearBuilt area(sqf)       price  replaceRoof sqfPrice


    1996         2395.0  $195000.00         true   $81.42

I tried using DecimalFormat but for some reason it didn't seem to work correctly when using it inside printf, when it worked normally in another area of my program. Any ideas on how to fix this?

2
  • 3
    Have a look at NumberFormat's predefined formats. Commented Apr 9, 2015 at 1:06
  • 2
    What a well written question! "Here's what I did, Here's what I'm Trying to do, and here's what I got." Wish there were more like this. Commented Apr 9, 2015 at 1:20

2 Answers 2

1

The problem is that you specify the price to be 11 digits fixed before the decimal, and sqfPrice to be 8 digits which cause the padding spaces.

If you decorticate your print statement :

System.out.printf("$%11.2f", 195000.0f);//print $  195000,0
System.out.printf("$%8.2f", 81.42f);//print $   81,42

You might want to use NumberFormat instead

NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);

Assuming you gave US locale,

currencyFormatter.format(195000)

would output $195,000.00.

Sign up to request clarification or add additional context in comments.

4 Comments

So I added NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(); and changed my printf to System.out.printf("\n%-10d%9.1f%11.2s%13s%8.2s\n", house1.getYear(),house1.getSquareFeet(),currencyFormatter.format(house1.getPrice()),house1.isRoofChangeNeeded(),currencyFormatter.format(house1.calcPricePerSqf())); and now I'm getting: image
@Kblo55 And what is wrong with this output ? The currency sign seems to be where you wanted it to be.
It did fix the dollar sign placement, but the numbers aren't correct. It's showing $1 and $8 instead of the correct amounts.
A friend helped me figure it out. I had to remove the .1 and .2 from my printf formatting for the two currency amounts because they were being converted to strings using the formatting. Thanks for your help!
0

It would be nice to be able to use the "$" sign with printf statements in java but we have to use the special class for that in java. The good news is that there are such available in the api.

Start with something like below.

DecimalFormat currencyFormatter = new DecimalFormat("$000000.00");
System.out.printf("\n%-10d%9.1f%11.2f%13s%8.2f\n",house1.getYear(),house1.getSquareFeet(),currencyFormatter.format(house1.getPrice()),house1.isRoofChangeNeeded(),currencyFormatter.format(house1.calcPricePerSqf());

Hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.