Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
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:
Tim Cooke
Campbell Ritchie
paul wheaton
Ron McLeod
Devaka Cooray
Sheriffs:
Jeanne Boyarsky
Liutauras Vilda
Paul Clapham
Saloon Keepers:
Tim Holloway
Carey Brown
Piet Souris
Bartenders:
Forum:
Beginning Java
formatting using printf
P Teng
Greenhorn
Posts: 16
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Can some one please explain the output caused by line 1
class FormatEx1 { public static void main(String[] args) { int i1 = 12345; int i2 = -12345; float f1 = -98.56f; String s1 = "hello world"; System.out.printf("%1$7d",i1); System.out.println(); System.out.printf("%1$(7f",f1); // line 1 System.out.println(); System.out.printf("%1$+2.5f",f1); System.out.println(); System.out.printf("%1$+(5d",i1); System.out.println(); System.out.printf("%1$+-5d",i2); System.out.println(); System.out.printf("%1$,-5d",i2); } }
output is below. What causes 559998 to appear after the . instead of just 56
12345 (98.559998) -98.56000 +12345 -12345 -12,345
Rob Spoor
Sheriff
Posts: 22849
132
I like...
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
The way floating point numbers are handled on computers. In short, just like you can't represent 1/3 exactly in decimal (0.3333 is an approximation), you can't represent 0.56 or even 0.1 exactly in binary. See
this
.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
Don't get me started about those stupid
light bulbs
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Formating with printf() and format()
format()
printf() doubt
Formatting with printf() and format
Formatter question
More...