C# - Numeric Suffixes [duplicate]
Possible Duplicate:
Declaration suffix for decimal type
Hey everyone,
In the following snippet of code; RewardValue is a decimal:
dto.RewardValue = 1.5;
Now, this gives me the following error:
"Cannot convert source type double to target type decimal"
Makes sense, 开发者_如何转开发and is easily fixable by changing that line of code to this:
dto.RewardValue = 1.5m;
Now, the "m" converts that to a decimal and all is good.
Does anybody know of somewhere where I could find a list of all those "m" type operators? (and if you could let me know what the proper term for those are, it would be greatly appreciated)
EDIT: Thanks to HCL and MartyIX for letting me know that these are referred to as "suffixes"
I believe the term you're looking for is "suffix".
Examples:
1; // int
1.0; // double
1.0f; // float
1.0m; // decimal
1u; // uint
1L; // long
1UL; // ulong
It's a pretty small list, really.
F: float
D: double
U: uint
L: long
UL: ulong
M: decimal
Of course a plain integral value by itself is interpreted as an int
, unless it's too big to be an int
in which case it's a long
, unless it's too big for a long
in which case it's a ulong
. If it's too big for a ulong
, you can't use it as a literal (as far as I know).
A value with a decimal point in it is automatically interpreted (as you found out for yourself) as a double
.
https://www.dotnetperls.com/suffix - they call it simply numeric suffixes
(http://msdn.microsoft.com/en-us/library/b1e65aza(VS.71).aspx - also suffix here)
Suffix type: unsigned int
Character: U
Example: uint x = 100U;
Suffix type: long
Character: L
Example: long x = 100L;
Suffix type: unsigned long
Character: UL
Example: ulong x = 100UL;
Suffix type: float
Character: F
Example: float x = 100F;
Suffix type: double
Character: D
Example: double x = 100D;
Suffix type: decimal
Character: M
Example: decimal x = 100M;
I believe it's called a "numeric litteral": http://www.blackwasp.co.uk/CSharpNumericLiterals.aspx
精彩评论