![](/img/trans.png)
[英]Combine two integers (one containing the integer part, the other the decimal part) into a floating point number
[英]Why is Parsing a String containing a floating point number to Decimal Allowed?
为什么是 :
string arr = "3.14";
decimal element = decimal.Parse(arr);
允许的
但:
decimal element = 3.14;
不允许
因为3.14
会被当作double
精度字面量,除非您另外指定。 并且没有从double
到decimal
隐式转换。
您可以使用m
后缀使其为十进制文字:
decimal element = 3.14m;
您可以参考C#语言规范 §2.4.4.2整数文字以获取有关文字后缀的更多信息。
请注意,在第一个代码中,您并没有直接将string
分配给decimal
, decimal
。 decimal.Parse
返回decimal
因此那里没有转换问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.