0
Which variable type is best for this?
Basically, the module I'm in is giving an example of a simple addition code and calling it a calculator (which it is, just limited to one function). But it uses integers, so that got me thinking: wouldn't it be better to use either floats or doubles? In which case, in a more complete calculator (lets go with addition, subtraction, multiplication, and division), would a float or a double be better? Actually, maybe my question would be better asked as "What's the difference between a double and a float?"
5 Respostas
+ 2
Far as I remember, you can't trust the floats. For high precision calculation (financial), you should stick to decimals.
Then again,for simple finance calculations, you can stick with just int.
int breadCostInCents = 120;
and only adjust on output.
0
double and float both contain -ve and decimal no.s but double has slight edge over float because of greater range . So its better to use double over float if u dont have a memory constraint in program .
0
@shubham sharma Thank you!
0
you better take double. float and double are similar type data types...but d dfrnce is double can hold more values than float
0
float and double are almost the same as float can be pretty precise but double is even more precise although either one works fine as they can both contain a large amount of precision