• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

String formatting doubt

 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Can anybody explain me the below program?

source: k&b

int i1=-123;
int i2=12345;
System.out.printf(">%1$(7d< \n", i1);
System.out.printf(">%0, 7d< \n", i2);
System.out.printf(">%+-7d< \n", i2);
System.out.printf(">%2$b + %1$5d< \n", i1,false);


output is:

> (123) <
>012,345<
>+12345 <
>false + -123<

Please explain me how it works



Thanks All
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
take a look at:
http://java.sun.com/javase/6/docs/api/java/util/Formatter.html#syntax

an in case you already did, what part troubles you?
[ May 07, 2007: Message edited by: John Stone ]
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,
My doubts are

1. Why the output is positive that is (123) in the first case

2. Why the output is 012,345 in the second case and i didnt understand the 3rd and the 4th output also.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


int i1=-123;
int i2=12345;
System.out.printf(">%1$(7d< \n", i1);
System.out.printf(">%0, 7d< \n", i2);





flags
1- "(" means enclose negative numbers inside the parenthesis
2- "0" says, there should be padding to this argument with zeroes
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi chandra,
In case of second output after 012 why "," has come.When the below line is executed output is +12345 i am not understanding can you explain me

System.out.printf(">%+-7d< \n", i2);
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nik arora:
Hi chandra,
In case of second output after 012 why "," has come.When the below line is executed output is +12345 i am not understanding can you explain me

System.out.printf(">%+-7d< \n", i2);



From K&B Page 490:


"," Use local-specific grouping separators (i.e. the comma in 123,456)

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"," was used to format the decimal.

Use :
System.out.printf(">%07d< \n", i2);

To get:
0012345
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,
Look at the below statement
i2=12345;
System.out.printf(">%0, 7d< \n", i2);

The output produced for this is 012,345 why after 012 the "," has come.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nik arora:
Hi Chandra,
Look at the below statement
i2=12345;
System.out.printf(">%0, 7d< \n", i2);

The output produced for this is 012,345 why after 012 the "," has come.



Grouping of digits
each comma after three digits from right.

1,234
12,345
123,456
1,234,567
12,345,678
123,456,789

And this is the way I get it, I don't remember why is so; but there is
some simple logic behind this for sure.

Thanks,
[ May 07, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The output produced for this is 012,345 why after 012 the "," has come.



I get the output as 12,345 instead of the 0 in front for the line System.out.printf(">%0, 7d< \n", i2);

I think its the Eclipse IDE or my installed compiler that is causing this issue. Just wanted to confirm that 0 should be included as per rules.

Thanks,
Megha
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Megha,

do this,

System.out.printf(">%0,7d< \n", i2);

You left one space between , and 7 so space included in padding.


Thanks,
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,
Thanks for the prompt response.

I think it was a typo in original question.

Thanks for clarification.I think Space is a new rule. One space is allowed and included before 0s used for padding. More than one space gives formatexception. But it wont be on exam

Thanks,
Megha
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Megha,
Why the "," is coming after 012?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nikhil,


There is no role of leading stuffed zeros in grouping of digits.



Thanks,
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not getting at all chandra
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nik arora:
HI Megha,
Why the "," is coming after 012?




as Chandra wrote:
[code]
Grouping of digits
[/code[

In some contries, there is a custom, that large numbers are written by using "separators" between digits, usually grouped by 3. This is mainly to improve - to easy reading of such big numbers for humans.

If I write
1000000000000
Can you tell how many zeroes are there? Yes of course, but you spend a while counting them.

If I write
1 000 000 000
you can immeadiatly tell, that there are 9 zeroes, you know that there are 3 digits in each group,.. so you just count groups.

In some countries, these separators may be different.
1,000,000,000 -> here "," is separating groups of digits.

If you are using Windows, go to Control Panel -> Regional and Language Options. And by the field of your settings, click customize and see "Digit grouping symbol".

I guess, that the whole point of "," used in Formatter class is that, it is separating groups of digits according to settings for your country (or the settings you are using, if those are different)
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI JOHN,

When the below statement is executed the output is
>+12345<. Can you explain me how?

i2=12345;
System.out.printf(">%+-7d< \n", i2);
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nik arora:
HI JOHN,

When the below statement is executed the output is
>+12345<. Can you explain me how?

i2=12345;
System.out.printf(">%+-7d< \n", i2);



That "-" is flag, that says to left justify the argument.

See the difference in output with/without using that flag "-"


Output:
> +12345<


Output:
>+12345 <



Thanks,
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks chandra got
 
The City calls upon her steadfast protectors. Now for a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic