0
What's the difference between int, float and double data types ?
I don't understand, int is for numeric value, so why is there two others data types ?
5 Réponses
+ 3
because sometimes you need to use numbers that are not integrer.
Like a price. lets say $5.25
or gravity 9.58
or terrain 589.6 Km.
So you need decimals part.
+ 1
So float has one decimal part and double has two decimals parts?
+ 1
if "float has one decimal part and double has two decimals parts ?In fact, they both use a "floating decimal point" with exponent. So not only a "decimal part", but too a very large numbers, with E38 (for float) and E308 for double. And the important is the precise number of digits of such data type. (7 digits in float, 15 in double). This is not much for such ranges.
If you want to calculate precise, you have to use another type, Decimal, with 28 precise digits..
So, this is the answer why we are using the different number types. Every type has own different precise of digits, the different size od memory and diferent speed of calculations too. Using the inproperly data type can cause a bad result or a unefective work
+ 1
int : whole numbers, positive or negative. The range of values depends on the size of the int. Usually 32 bits or 4 bytes of memory, the range is -2^31 to 2^31 - 1 (about -2 billion to +2 billion).
float : allows fractional parts (digits after the decimal point) over a much greater range than int. Usually 32 bits or 4 bytes of memory.
double : higher precision than float (more digits after the decimal point) and a greater range of values. Usually 64 bits or 8 bytes of memory.
Which you need depends on the situation.
+ 1
Thanks ! By the way the community is really active, it help a lot!