[英]Why is Parsing a String containing a floating point number to Decimal Allowed?
Why is : 为什么是 :
string arr = "3.14";
decimal element = decimal.Parse(arr);
Allowed 允许的
But: 但:
decimal element = 3.14;
Not allowed 不允许
Because 3.14
is treated as a double
literal unless you specify otherwise. 因为3.14
会被当作double
精度字面量,除非您另外指定。 And there is no implicit conversion from double
to decimal
. 并且没有从double
到decimal
隐式转换。
You can use m
suffix to make it a decimal literal: 您可以使用m
后缀使其为十进制文字:
decimal element = 3.14m;
You can refer to C# Language Specification §2.4.4.2 Integer Literals for more info about literal suffixes. 您可以参考C#语言规范 §2.4.4.2整数文字以获取有关文字后缀的更多信息。
Note that in the first code you are not directly assigning a string
to a decimal
, decimal.Parse
returns decimal
so there is no conversion issue there. 请注意,在第一个代码中,您并没有直接将string
分配给decimal
, decimal
。 decimal.Parse
返回decimal
因此那里没有转换问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.