Debis
Debis

Reputation: 23

How to use scientific operator E with Variables?

I declared a double variable as below:

double x=56.27d

And then all I tried to do is the following: (56.27*10*10)

System.out.println(xE2);

And this is not working.

Upvotes: 2

Views: 649

Answers (4)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79085

Do it as follows:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        double d = sc.nextDouble(); // Get input from the user
        System.out.println(d * 10 * 10);

        // Displaying it using scientific notation
        // Format the number rounded up to two places after decimal and into scientific notation
        System.out.println(String.format("%.2e", d * 10 * 10));

        // Examples of e or E with double literals
        double x = 1e2; // 1 * 10 to the power of 2 = 100.0
        System.out.println(x);
        double y = 1E2; // 1 * 10 to the power of 2 = 100.0
        System.out.println(y);
        double z = 1e+02; // 1 * 10 to the power of 2 = 100.0
        System.out.println(z);
        System.out.println(d * 1e2);// i.e. d * 100.0
    }
}

A sample run:

Enter a number: 56.27
5627.0
5.63e+03
100.0
100.0
100.0
5627.0

Notes:

  1. Check this to learn more about Formatter.
  2. E or e can be used only with double literals, not double variables.

Feel free to comment in case of any doubt/issue.

Upvotes: 1

matt
matt

Reputation: 12346

There isn't an 'E' operator in java. This would conflict with variable names for one thing. double xE2=x*1e2; In this case, xE2 is a variable name, but I do use 1e2 as a java literal.

Upvotes: 2

Guy_g23
Guy_g23

Reputation: 385

I suggest you to read java.util.Math library documentation, it includes many scientific functions like Exponents: https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html

Upvotes: 0

Anikin Skywalker
Anikin Skywalker

Reputation: 35

You shouldn't use the exponent notation since it is a part of double literal. Try multiplying.

System.out.println(x * 10 * 10)

Upvotes: 0

Related Questions