Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 this is in different scenarios ?

like image 792
nandu.com Avatar asked Mar 27 '11 19:03

nandu.com


3 Answers

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;

like image 74
Andrew Orsich Avatar answered Oct 10 '22 10:10

Andrew Orsich


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.

like image 34
Wannabe Avatar answered Oct 10 '22 11:10

Wannabe


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 ;-) ).

like image 44
Aasmund Eldhuset Avatar answered Oct 10 '22 10:10

Aasmund Eldhuset