469,270 Members | 1,063 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

Printing large double values without the exponential notation? - Java

Hi all,

I have a requirement to print large double values without the exponential notaion. The double value can be a declared variable or any calculated value within the code. In any case, I should be able to print the result without containing the exponential notation. Please help me out.
Oct 27 '07 #1
4 40047
JosAH
11,448 Expert 8TB
Have a look at the DecimalFormat class; it can do the job.

kind regards,

Jos
Oct 27 '07 #2
Hi,

I tried to use the java.text.DecimalFormat class in the following sample code:

[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));
}
}
[code]

I got the o/p as: 1234567890123456800000.0. Any more suggestions on this...
Oct 28 '07 #3
JosAH
11,448 Expert 8TB
Hi,

I tried to use the java.text.DecimalFormat class in the following sample code:

Expand|Select|Wrap|Line Numbers
  1. import java.text.DecimalFormat;
  2.  
  3. public class prog2
  4. {
  5. public static void main(String args[])
  6. {
  7. String str = "1234567890123456789000.1234567890";
  8. Double d = new Double(str);
  9. double dd = d.doubleValue();
  10. dd = dd + 123.0;
  11. DecimalFormat formatter = new DecimalFormat("##0.0######");
  12. System.out.println(formatter.format(dd));
  13. }
  14. }
  15.  
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.
Oct 28 '07 #4
Hi all,

I have a requirement to print large double values without the exponential notaion. The double value can be a declared variable or any calculated value within the code. In any case, I should be able to print the result without containing the exponential notation. Please help me out.
double d = 0.0000005;
System.out.println(new DecimalFormat("#.#######################").format( d));
Oct 30 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Timothy Fitz | last post: by
4 posts views Thread by ai lian | last post: by
4 posts views Thread by MFRASER | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.