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

Explain me .

 
Ashok Pradhan
Ranch Hand
Posts: 180
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
 
Amit Y Desai
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • 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).
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic