wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Explain me . Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Explain me ." Watch "Explain me ." New topic
Author

Explain me .

Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180



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

Joined: Dec 06, 2007
Posts: 35
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

Joined: Dec 06, 2007
Posts: 35
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

Joined: Oct 30, 2001
Posts: 1970
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).


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
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


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Explain me .
 
Similar Threads
AWT: Painting buttons
Generic method question
getDouble() not returning correct data
to yield() or not to yield()?
Increase the range of Double