I am using the first way and my new company is using the second way.
double x = 1.99;
double y = 9.02D;
which one is correct and why? and If both is correct, then how to use this is in different scenarios ?
From msdn:
By default, a real numeric literal on the right-hand side of the assignment operator is treated as double. However, if you want an integer number to be treated as double, use the suffix d or D, for example: Copy
double x = 3D;
So there is no difference between double x = 1.99;
and double y = 9.02D;
I would say the way your company is doing it is probably safer. The D after the value 9.02 forces the integer numeric (real) value 9.02 to be treated as a double.
A floating-point literal without a suffix is a double
, so there's really no difference between 1.99
and 1.99D
(or 1.99d
) - except that the latter form makes it explicit that this is, indeed, a double
. So it's a matter of style, really. In general, you should of course stick to your company's style (unless you have a really compelling reason that the style is "wrong" - in which case you should convince the company to change the style, rather than just silently violating it yourself ;-) ).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With