• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Explain me .

 
Ranch Hand
Posts: 180
Netbeans IDE Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


When i run this program i got the answer 1.0E-5,i know that 1.0E-5=0.00001,but how could i get the result 0.00001
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Ashok,

i faced with same problem previously but could not find any solution.
If you are having double value starting with "0." followed by three 0's then resulting format in sysout would not be in form 0.0000XXXX but would be in format XXE+-XXX
 
Amit Y Desai
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Infact there is solution

Try to use the method DecimalFormat.format(double), inherited from the class NumberFormat.

DecimalFormat myFormatter = new DecimalFormat(pattern);
String output = myFormatter.format(value);
System.out.println(value + " " + pattern + " " + output);

pattern can be for example "###.###" or "###,###.###"

You should also read the Java Documentation pages on DecimalFormat class.

go to sun api docs..click here
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The solution above is appropriate. Basically, the default way of printing floating point numbers (double and float) is only intended for debugging, not nicely-formatted output. You should use something like a DecimalFormat to present floating-point numbers to the user.

Be aware, however, that floating-point arithmetic is not exact. This is nothing to do with Java; it's the same in all languages. Floating-point should be used where you need to store numbers with a wide range of value in a smallish, fixed amount of memory and fast computation and where approximation is acceptable.

Where you need total accuracy, you should not use floating-point. You should either use integers (int, long etc.), scaled appropriately (e.g. use an integer number of thousandths), or use the BigDecimal class. BigDecimal can represent quite a big range (not as big as floating-point), but uses much more memory than floating-point and is also slower to compute.

Currency (money) calculations are the most common example of where floating-point is inappropriate. Never use floating-point for currency. Usually, the best thing to do is to use integers, representing all amounts as the smallest unit in the currency (e.g. cents, pence).
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also you could use



http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintStream.html#printf(java.util.Locale,%20java.lang.String,%20java.lang.Object...)

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic