G'day everyone,

I'm doing at a tutorial on Oracle website and still following it so far. I have come across this formatter "%,8d" and can't seem to work out how it works. I know it's adding a coma to the integral number. The thing that I can get my head around is the decimal place isn't specified. I wonder how the compiler knows where to put the comma?
The last line is used to add a plus sign. Well, what If I wanna add a minus sign?????
"%-" that didn't work for me.

long n = 461012;
      // -->  " 461,012"
      System.out.format("%,8d%n", n);
      //  -->  "+461,012"                
      System.out.format("%+,8d%n%n", n);


Recommended Answers

All 4 Replies

Did you check the API?

Brilliant Question!!! Yes, I did. Actually, that is the website I was on.

Did you check the API?

I have found some good explanations so now I know why it formats the number after three decimal places. it's because "%, .3" which means put a comma after three decimal places and if I wanna add minus, I only need to use "%+". So, if the number being formatted is a minus number then the formatter will add the sign to it.

I hope this helps whoever looks up this information. Unfortunately, It seems like some people know the answers to these questions but they tend not to give them away. Gosh, it'd cost heaps and heaps of time and effort to explain them to beginners like me. Lol

Well, I would like to mark the thread as solved so the other person will get +1. But, I think that is not worth it at present because I found out the answers by myself.

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.