File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Decimal digits Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Decimal digits" Watch "Decimal digits" New topic
Author

Decimal digits

Abder-Rahman Ali
Ranch Hand

Joined: May 22, 2008
Posts: 138
In Java, when I do this for example:

0.75 - 0.25

I get: 0.5

How can I get: 0.50

Thanks.
[ June 24, 2008: Message edited by: Abder-Rahman Ali ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Realize that the answer, and how you display the answer, are two different things. Numbers don't have a certain number of digits; it's just how you display them that counts.

Use the java.text.DecimalFormat class, or use the System.out.printf() method. Both are well- described in their Javadocs.


[Jess in Action][AskingGoodQuestions]
vas reddy
Greenhorn

Joined: Jun 11, 2008
Posts: 23
try this...
but final result is in String you need to convert that into double

import java.text.*;
public class Value {
public static void main(String args[])
{
double x = 0.75;
double y = 0.25;
double z = x-y;
System.out.println("z =" + z);

NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
String s = nf.format(z);

System.out.println("s =" + s);

}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Decimal digits
 
Similar Threads
assignments: too easy?
float value ?
K&B Math.round() question
RANDOM NUMBERS
question regarding mulitple choice questions on the SCJP exam