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

printf

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


It prints:
total 1278944

I want it prints 12..
 
Campbell Ritchie
Sheriff
Pie
Posts: 50206
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.printf("%s %s %n", "total", 12);???
 
Campbell Ritchie
Sheriff
Pie
Posts: 50206
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fact that you tell the %d tag to use 2 spaces will not make it truncate the number. It would be a far worse error to truncate the number than to spoilt the formatting. You can try repeated division
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:
total 1278944

I want it prints 12..


Do you want to show the number of 100,000s there are? Or do you want to show the first two digits? Or some other rule?

To put it another way, what would you want to to print for each of these values?

1
12
1278
127894
127894456789
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you want to show the number of 100,000s there are? Or do you want to show the first two digits?


First two digits.
 
James X Peterson
Whizlabs Java Support
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi abalfazl hossein,

first you find the syntax for the format specifiers so that you will know the what to do.Following link will help you

http://sharkysoft.com/archive/printf/docs/javadocs/lava/clib/stdio/doc-files/specification.htm

Regards,
James
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:
Do you want to show the number of 100,000s there are? Or do you want to show the first two digits?


First two digits.

convert it to a string, then take the substring?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:
Do you want to show the number of 100,000s there are? Or do you want to show the first two digits?


First two digits.


So that's first 2 digits regardless of the scale?

1 --> 1
12 --> 12
1278 --> 12
127894 --> 12
127894456789 --> 12

Yes?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, then, as Fred said: convert it to a string, then take the substring
 
Campbell Ritchie
Sheriff
Pie
Posts: 50206
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a 1‑digit number, you would have to pad it with 0s from the left.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If thes string be "total"" and I wanted it is printed as "to", What must I do?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15483
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:If thes string be "total"" and I wanted it is printed as "to", What must I do?

Jeff Verdegan wrote:Okay, then, as Fred said: convert it to a string, then take the substring
 
Campbell Ritchie
Sheriff
Pie
Posts: 50206
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:If thes string be "total"" and I wanted it is printed as "to", What must I do?
That shows why the % tags should not change their argument. You are not printing the value in a different format, but a different value. "to" and "total" are different, real, words.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic