为什么 C# 小数不能在没有 M 后缀的情况下初始化?

发布于 2024-11-28 01:47:09 字数 463 浏览 0 评论 0原文

该代码

public class MyClass
{
    public const Decimal CONSTANT = 0.50; // ERROR CS0664
}

产生此错误:

错误CS0664:双精度类型的文字无法隐式转换为 输入“十进制”;使用“M”后缀创建此类型的文字

如记录。但这是有效的:

public class MyClass
{
    public const Decimal CONSTANT = 50; // OK
}

为什么他们禁止第一个?我觉得很奇怪。

The code

public class MyClass
{
    public const Decimal CONSTANT = 0.50; // ERROR CS0664
}

produces this error:

error CS0664: Literal of type double cannot be implicitly converted to
type 'decimal'; use an 'M' suffix to create a literal of this type

as documented. But this works:

public class MyClass
{
    public const Decimal CONSTANT = 50; // OK
}

Why did they forbid the first one? It seems weird to me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

卖梦商人 2024-12-05 01:47:10

来自浮点数字类型

浮点类型和十进制类型之间没有任何隐式转换;因此,必须使用强制转换来在这两种类型之间进行转换。

他们这样做是因为 double 的范围非常大,从 ±5.0 × 10−324 到 ±1.7 × 10308,而 int 只有 -2,147,483,648 到 2,147,483,647。小数的范围是 (-7.9 x 1028 到 7.9 x 1028) / (100 到 28),因此它可以容纳int 但不是双精度数。

From Floating-point numeric types:

There isn't any implicit conversion between floating-point types and the decimal type; therefore, a cast must be used to convert between these two types.

They do this, because double has such a huge range ±5.0 × 10−324 to ±1.7 × 10308 whereas int is only -2,147,483,648 to 2,147,483,647. A decimal's range is (-7.9 x 1028 to 7.9 x 1028) / (100 to 28), so it can hold an int but not a double.

自由如风 2024-12-05 01:47:09

不带 m 后缀的文字类型是 double - 就这么简单。您也不能以这种方式初始化 float

float x = 10.0; // Fail

文字的类型应该从文字本身中明确,并且它分配给的变量的类型应该可以分配给 from 该文字的类型。因此,您的第二个示例之所以有效,是因为存在从 int (文字类型)到 decimal 的隐式转换。没有从 doubledecimal 的隐式转换(因为它可能会丢失信息)。

就我个人而言,如果没有默认值或者默认值是十进制,我会更喜欢它,但这是另一回事......

The type of a literal without the m suffix is double - it's as simple as that. You can't initialize a float that way either:

float x = 10.0; // Fail

The type of the literal should be made clear from the literal itself, and the type of variable it's assigned to should be assignable to from the type of that literal. So your second example works because there's an implicit conversion from int (the type of the literal) to decimal. There's no implicit conversion from double to decimal (as it can lose information).

Personally I'd have preferred it if there'd been no default or if the default had been decimal, but that's a different matter...

浅沫记忆 2024-12-05 01:47:09

第一个示例是双重文字。第二个示例是整数文字。

我想在不损失精度的情况下不可能将双精度数转换为十进制数,但使用整数就可以了。因此它们允许使用整数进行隐式转换。

The first example is a double literal. The second example is an integer literal.

I guess it's not possible to convert double to decimal without possible loss of precision, but it is ok with an integer. So they allow implicit conversion with an integer.

悲凉≈ 2024-12-05 01:47:09

每个文字都被视为一种类型。如果您不选择“M”后缀,它将被视为双精度。您不能隐式double转换为decimal 是很容易理解的,因为它会失去精度。

Every literal is treated as a type. If you do not chose the 'M' suffix it is treated as a double. That you cannot implicitly convert a double to a decimal is quite understandable as it loses precision.

余生一个溪 2024-12-05 01:47:09

您提供的同一链接中的答案稍低一些,而且也是 此处。在转化中:

整数类型隐式转换为小数,结果计算为小数。因此,您可以使用整数文字来初始化十进制变量,无需后缀。

所以,原因是因为int和decimal之间的隐式转换。由于 0.50 被视为双精度,并且双精度和十进制之间没有隐式转换,因此您会得到错误。

有关更多详细信息:

http://msdn。 microsoft.com/en-us/library/y5b434w4(v=vs.80).aspx

http://msdn.microsoft.com/en-us/library/yht2cx7b.aspx< /a>

Your answer is a bit lower in the same link you provided, and it is also here. In Conversions:

The integral types are implicitly converted to decimal and the result evaluates to decimal. Therefore you can initialize a decimal variable using an integer literal, without the suffix.

So, the reason is because of the implicit conversion between int and decimal. And since 0.50 is treated as double, and there is not implicit conversion between double and decimal, you get your error.

For more details:

http://msdn.microsoft.com/en-us/library/y5b434w4(v=vs.80).aspx

http://msdn.microsoft.com/en-us/library/yht2cx7b.aspx

向地狱狂奔 2024-12-05 01:47:09

这是 C# 的创建者做出的设计选择。

可能是因为 double 可能会丢失精度,并且他们不希望您存储该损失。 int 没有这个问题。

It’s a design choice that the creators of C# made.

Likely it stems from double can lose precision and they didn't want you to store that loss. int doesn’t have that problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文