Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

double primitive in two forms  RSS feed

 
Rizwan Mohammad
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i print double sometimes it prints in exponential format sometimes in normal way.. what exactly is the rule behind this printing?
what is the largest number that will be printed in the normal way ?
 
Petrus Pelser
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you give some examples of when you encounter this?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you just print the number without specifying how it should be formatted, Java just uses the default formatting. If you don't want that, specify the formatting yourself.

If you are using Java 1.5 (or newer) you can use System.out.printf(...) with a format string to specify how you want your number formatted.

If you're using Java 1.4 (or older) you can use class java.text.DecimalFormat.

To answer your original question about the default formatting: you can find the answer in the API documentation of method java.lang.Double.toString(double), it explains in detail how the default formatting works.
 
Rizwan Mohammad
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper. Its clear now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!