I have problem formating this code to displey 2 decimal points

public String toString() 
      {
         return super.toString() + "\n\tYear Level:\t" + getYearLevel() + "\n\tTuition:\t$" + "%1$.2f",calcTuition();
      
      }

calcTuition is type double

how can I do this?

Thanks

Recommended Answers

You really only need the "%.2f" part. You can read all about the formatting options here: http://docs.oracle.com/javase/6/docs/api/java/util/Formatter.html#syntax

Jump to Post

All 4 Replies

You can use the String.format() method to apply those formats.

public String toString() 
      {
         return super.toString() + "\n\tResearch Area:\t" + getResearchArea() + "\n\tTuition:\t$" + String.format("%.2g%n", calcTuition());
      
      }

like that I get number like ; $7.2e+03
dont get it?

got it :D

But can some tell me where can I find more info about it?

public String toString() 
      {
         return super.toString() + "\n\tYear Level:\t" + getYearLevel() + "\n\tTuition:\t$" + String.format("%.2f%n", calcTuition());
      
      }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.