permaculture playing cards*
The moose likes Java in General and the fly likes printf Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "printf" Watch "printf" New topic
Author

printf

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


It prints:
total 1278944

I want it prints 12..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
System.out.printf("%s %s %n", "total", 12);???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Sep 06, 2007
Posts: 635
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

Joined: Feb 26, 2013
Posts: 158
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

Joined: Oct 02, 2003
Posts: 11160
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Sep 06, 2007
Posts: 635
Yes!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Okay, then, as Fred said: convert it to a string, then take the substring
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
For a 1‑digit number, you would have to pad it with 0s from the left.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
If thes string be "total"" and I wanted it is printed as "to", What must I do?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
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.
 
 
subject: printf
 
Similar Threads
Flow control (For loop)
Please explain the output of this: This question is from ExamLabs Diagnostic Test
The tricky for-loop
Please tell me why it prints 0
System.out.printf("%1$c",65536);