开发者

What is the correct way of representing double in c#?

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 t开发者_C百科his 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 ;-) ).


For a simple declaration, it seems extraneous.

The 'd' suffix could be useful in situation such as this:

int n = 10;
double a = n / 3; // 3
double a = n / 3d; // 3.333..

But it's poor form anyway, since the second division depends on implicit cast of (n) to a double before doing the division.

Same reasoning if you were passing a floating constant to a method that happened to have two overloads:

void foo(float bar);
void foo(double bar);

you could distinguish the version you want to call by calling:

X.foo(3.0D);

Again, not the best form.

As for simple declaration, there's no speed advantage since the compiler will likely optimize it anyway.

(borrowed example from here)


The double y = 9.02D;, the suffix D converts your numeric (real) value to a double. This is used if you want your integer value to be converted to a double or to specify that your value is indeed a double.

E.g.

double d = 3D;

Reference.

PS. As Sehe mentioned, this would convert your variable d to a double.

var d = 3D;

without the D suffix, d would have been an int.


Stylistically, you generally only need to add a 'd' or a 'D' when you want an integer literal to be a double:

double d = 1d;

So in your example, making double d = 1.99d, is pointless, because the compiler will already assign 1.99 to a double. Not to mention when you declared the d, it's type was double as well.

In general, you don't need to add d or D to double literals.

However, in your case the 2nd one is correct, because it's the style chosen by the people who are paying you, and in the end, that's what really matters.


Suffix d is important while using reflection and other dynamic usage to enforce complier to consider the number as double instead of integer where double can have number without decimal. 1.99 is perfect double.

While using reflection using 1 will give conversion error.

PropertyInfo p...

p.SetValue(obj, 1)

will throw error but

p.SetValue(obj, 1D)

will work correctly.

var x = 1; 

x is int

var x = 1D;

x is double.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜