Member Avatar for mehnihma

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

All 4 Replies

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

Member Avatar for mehnihma
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?

Member Avatar for mehnihma

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.