# Decimal digits

Abder-Rahman Ali
Ranch Hand
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
Posts: 24212
35
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.

vas reddy
Greenhorn
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);

}
}