Getting scientific with floats and doubles - Java Language Basics

Java examples for Language Basics:float

Introduction

If you have a scientific mind, you may want to use scientific notation when you write floating-point literals.

Note that the exponent can be negative to indicate values smaller than 1.

For example, double e = 5.10e+6; This equation is equivalent to double e = 5100000D;

Demo Code

public class Main {
  public static void main(String[] args) {
    double e = 5.10e+6;
    System.out.println(e);// ww  w.  j  a  va 2s .com
    e = 5100000D;
    System.out.println(e);
  }

}

double impulse = 23e-7; is equivalent to double impulse = 0.0000023;

Demo Code

public class Main {
  public static void main(String[] args) {
    double impulse = 23e-7;
    System.out.println(impulse);//from   w  w  w. j a  v  a 2  s  . co m
    impulse = 0.0000023;
    System.out.println(impulse);
  }

}

The sign is optional if the exponent is positive, so you can also write: double e = 5.10e6;

Demo Code

public class Main {
  public static void main(String[] args) {
    double e = 5.10e6;
    System.out.println(e);/*from   w  w w .j ava 2 s  .  c o m*/
  }

}

If you omit the suffix, D is assumed. As a result, you can usually omit the D suffix for double literals.


Related Tutorials