Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decide whether the given value is float or double in Java?

Tags:

java

What is the data type for the number 9.6352 in Java?

Actually if I postfix the number with 'd' or 'f'. then it will be quite easy to decide. But if I leave it as alone, then which will be the default type of the number in java?

like image 561
Developer Avatar asked Nov 22 '11 18:11

Developer


People also ask

How do you know if a number is a float or a double in Java?

Quote from Java Language Specification #3.10. 2: A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise, its type is double and it can optionally be suffixed with an ASCII letter D or d.

How do you identify it is float or double?

float has 7 decimal digits of precision. double is a 64-bit IEEE 754 double precision Floating Point Number – 1 bit for the sign, 11 bits for the exponent, and 52* bits for the value. double has 15 decimal digits of precision.

What is the difference between a double and a float in Java?

Size: Float is of size 32 bits while double is of size 64 bits. Hence, double can handle much bigger fractional numbers than float. They differ in the allocation of bits for the representation of the number.

Is 3.14 double or float?

Explanation: Given 3.14 is a double constant.


2 Answers

Go check out the JLS section on floating point literals (§3.10.2):

A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d.

So it's a double if it's not suffixed.

like image 118
Mat Avatar answered Sep 27 '22 20:09

Mat


By default, any floating point data type without 'f' or 'F' postfixed, is considered as of type double. Since the given number doesn't end with 'f'/'F', it is of type double.

We can optionally add 'd' or 'D' at the end to mark it as a double type, but again, it is optional.

like image 28
nitin1706 Avatar answered Sep 27 '22 20:09

nitin1706