We help IT Professionals succeed at work.

Scientific Notation

Angela030399 asked
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.


Watch Question

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

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.

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.

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



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


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
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:


Cheers Tom

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");

This print out


Thanks, Tom7

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