HomeJava

Java for Testers – Expressing floating-point literals in standard and scientific ways

Java for Testers – Expressing floating-point literals in standard and scientific ways

<<Previous Post                    << Complete Tutorial>>                        Next Post>>

In the previous article, I have explained assigning different types of literals to the char data type variable.

In this article, I am going to explain the different ways of expressing the floating-point literals.

The following are the two ways for expressing floating-point literals:

  1. standard way
  2. scientific way

Standard way of expressing the floating-point expression:

As we already know about the standard ways of expressing the floating-point expressions (explained or used in the previous articles).

Example:

float a = 32.569f;

Scientific way of expressing the floating-point expressions:

Scientific way uses floating-point number plus a suffix. The suffix specifies the power of 10.

And to represent a floating-point value we add f letter at the end of the floating-point literal.

And to represent the exponent value we have to add the letter E before the suffix.

For example:

float a = 3.567E2f;

In the above example,

  1. The letter f is for representing the value as floating-point value.
  2. The letter E is for representing the exponent value
  3. The letter 2 after E is the suffix (2 means 10×10 i.e. 100)

3.567E2 is nothing but 3.567x(10×10)

i.e. nothing but 3.567×100

Which will ultimately become 356.7

The below program practically demonstrate the scientific representation of floating-point value in Java:

float sceintific - demo

Below is the code used in the above demonstration:

public class Demo {

	public static void main(String[] args) {
		
		float a = 3.568E2f;
		
		System.out.println(a);
		
		
	}

}

Note: We can also use this scientific representation with the double data type in a similar way. In the case of double data type, we don’t have to use the letter f at the end of the literal.

Example:

double a = 3.568E2;

Here conclude this article on expressing the floating and double type literals in a scientific way.

In the next article, I will explain the remaining things about data types.

Next Steps:

  • > To learn more about Java, continue to the next post (Click on Next Post link below)
  • > Check complete Java Tutorial Contents here (Click here)

Please leave your questions/comments/feedback below:

Happy Learning ?

Arun Motoori (www.QAFox.com)

On a mission to help the Testing Community in all possible ways.

<<Previous Post                    << Complete Tutorial>>                        Next Post>>

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *

For FREE Testing Tutorials & Videos

X
Open chat
Contact Us on Whatsapp