Hi,
I tried to use the java.text.DecimalFormat class in the following sample code:
-
import java.text.DecimalFormat;
-
-
public class prog2
-
{
-
public static void main(String args[])
-
{
-
String str = "1234567890123456789000.1234567890";
-
Double d = new Double(str);
-
double dd = d.doubleValue();
-
dd = dd + 123.0;
-
DecimalFormat formatter = new DecimalFormat("##0.0######");
-
System.out.println(formatter.format(dd));
-
}
-
}
-
I got the o/p as: 1234567890123456800000.0. Any more suggestions on this...
A double number can only store +- 15 significant digits (with a decimal dot somewhere,
that's why it's called a 'floating point number'). Your example contains way too
many significant digits, i.e. they can't all be stored in a double. The number, say,
1.23e45 would've been fine (less than 15 significant digits).
If you want numbers like that you need the
BigDecimal class.
kind regards,
Jos
ps. In the Java Articles section there's an 'index' article containing a link to the
paper "What every computer scientist should know about floating point arithmetic".
It's a must read.