We help IT Professionals succeed at work.

Scientific Notation

Angela030399
Angela030399 asked
on
How do write a method called say 'numValue' that outputs a value of type 'double' in the e notation format( Example: 1.23e-45).

New to java. Please be specific.


Thanks,

Angela
Comment
Watch Question

Commented:
Hi Angela

here's a method I wrote some time ago:

public static String toScientific(double d) {
  int exp = (int)Math.floor(Math.log(d)/Math.log(10.0));
  double fraction = d / Math.pow(10.0, exp);
  return ""+fraction+"E"+exp;
}

I hope it works fine.

Cheers Tom

PS: 'double' sometimes produces strange results, like 5.499999999999999E-9  instead of 5.5E-9. I guess this depends on your Computer/OS. If you want to avoid that I guess you'll have to find another, more complex algorithm.
Jim CakalicSenior Engineer

Commented:
Formatted numeric output can be achieved using the java.text.DecimalFormat class. The javadoc provides details on the pattern syntax and specification of patterns and use of methods for formatting scientific notation. There is also a Technical Tip on the JDC that provides additional explanation and examples.
    http://java.sun.com/j2se/1.3/docs/api/index.html
    http://developer.java.sun.com/developer/TechTips/2000/tt0411.html#tip1

If you are familiar with the C printf facility for formatted output you might also consider using the PrintFormat class also on the JDC. Although the article indicates that JSR-51 included adding a printf-like facility to the Java platform, this doesn't seem to have happened in JDK 1.4 even though much of the NIO spec seems to have been implemented.
    http://developer.java.sun.com/developer/technicalArticles/Programming/sprintf/

A similiar functionality which I like better is also provided in an implementation by Dave Astels of Saorsa Development. The link that should get to the code is currently not working but I would be happy to post it, with a few changes that I have made, if you are interested.

Best regards,
Jim Cakalic

Author

Commented:
Tom7,

Could you explain these two lines to make sure I understand it right.

int exp = (int)Math.floor(Math.log(d)/Math.log(10.0));
double fraction = d / Math.pow(10.0, exp);

Thanks, Angela

Commented:
Hi,

let me try to explain.

The int 'exp' stands for how many digits I want to shift the floating point. This is achieved by the logarithm (base 10).

Let's take the number 200 (=200.00) as a simple example: The logarithm (base 10) of 200 is 2.30102.... In other words, 10^2.30102... = 200 . So I shift the floating point two digits to the left: 200.00 --> 2.0000. The line

double fraction = d / Math.pow(10.0, exp);

does just that; mathematically, shifting a floating point is equivalent to a division (or a multiplication) by a power of 10. Divide any number by 10^n, and the floating point will be shifted n digits to the left.


Now in the first line I just take the logarithm (base 10) of the original number and take the 'floor' of it. Unfortunately, Java does not have a base-10-logarithm (it only has the natural logarithm, log_e). I use a common logarithm rule to get the base-10-logarithm:

log_10 x = (log_e x) / (log_e 10)




So far so good. But why does one get the exponent that way? Well, the base-10-logarithm is just the 'opposite' of '10 to the power of ...'. Example:

10 to the power of -3 is equal to 0.001
so
the base-10-logarithm of 0.001 is -3

and -3 is exactly the exponent we are looking for, because

0.001 = 1 E-3




Well, I'm not sure if this helps ... if you have some mathematical background you will see the connection between scientific notation and base-10-logarithms.

I also found some links where things are explained in more detail:

http://physics.mtsu.edu/~wmr/log_2.htm
http://www.rain.org/~rcurtis/logs.html
http://www.astronomynotes.com/mathrev/s3.htm


Cheers Tom

Commented:
You can pass the pattern "0.###E0" to DecimalFormat constructor.
The number of '#' indicate the decimal place.

java.text.DecimalFormat df = new java.text.DecimalFormat("0.###E0");
System.out.println(df.format(0.00125));

This print out
1.25E-3

Author

Commented:
Thanks, Tom7

Explore More ContentExplore courses, solutions, and other research materials related to this topic.