Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

printf method help

 
Kamila Jolan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I have a problem understanding how the formatting in the printf method is working.
The following method:
gives the output: +07 252. Why it is not: +07,252? Why it does not display ","?
Also returns: > (123)<, Why it does not display a sign (-)?

Any help will be appreciated

Kamila
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.printf("%1$,+07d",7252);

have you checked the output by running it.it must show you.
and remember below thing while using printf
-ve means left justify this argument.
( - this wil put the negative numbers into the brackets.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have a problem understanding how the formatting in the printf method is working.
The following method:
gives the output: +07,252


Also
returns: > (123)< it will not show -ve because when we include "(" then it will put negative numbers into it.


Study chapter 6 last page before 2 minute drill for the K&B you will get the complete explaination of your query
 
Vicky Mehta
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Minor correction in output
int i1 = -123; System.out.printf(">%1$(7d< \n", i1);

will be
Note 2 leading spaces before open braces
 
Kamila Jolan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vicky, Shanky thanks for the answer.
I understand the second case, but still I don't get why in the in the first case the "," is not displayed.Could you elaborate more please?

Regards,

Kamila

 
Vicky Mehta
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the ',' ('\u002c') flag is given, then the locale-specific grouping separator is inserted by scanning the integer part of the string from least significant to most significant digits and inserting a separator at intervals defined by the locale's grouping size.

http://download.oracle.com/javase/6/docs/api/java/util/Formatter.html#syntax
, indicates that the digits in the number should use the default digit seperator of the system locale. It is well possible that the system locale of you computer is set to such locale in which the digit seperator is a space. hence, your display is like like that.
There are two options to change that:
1) Change default system locale in the system in Control Panel -> Regional and Language Options to such a locale in which the seperator is a comma. Remember to reinvoke the command prompt and then execute your program.
2) Invoke following printf
http://download.oracle.com/javase/6/docs/api/java/io/PrintStream.html#printf(java.util.Locale,%20java.lang.String,%20java.lang.Object...)
instead of
http://download.oracle.com/javase/6/docs/api/java/io/PrintStream.html#printf(java.lang.String,%20java.lang.Object...)
for the first parameter you can Locale as LOCALE.US for which AFAIK the seperator is a comma.
 
Kamila Jolan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vicky thanks very much
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic