Floating Point Data Types Overview

Floating-point Literals

A floating-point literal is of type float if it ends with the letter F or f, otherwise it is of type double and can optionally end with the letter D or d.

Double Type Literal

Let's declare a double floating point

double averageScore = 550.50; 
double totalScore = 975.05;

We could also have optionally prefixed the literal with the D or d, but this is usually omitted since its the default.

double averageScore = 550.50D; 
double totalScore = 975.05d;

Float Type Literal

The letter F or f is required when assigning literal values of the float type. If you miss the letter F or f, this will result in a syntax error.

float averageScore = 550.50F; //Here the letter F is required
float totalScore = 975.05f; //Also here the letter f is required.

You can also use scientific notation for the literal value of the float.

float averageScore = 0.5505E3F;
float totalScore = 0.97505e3f;

We can also use scientific notation in declaring literal values of type double.

double averageScore = 0.5505E3; //We left the D or d since its optional
double totalScore = 0.97505e3;

results matching ""

    No results matching ""