为什么不能在不使用类型后缀的情况下直接将带小数点的数字分配给十进制类型?

时间:2023-01-16 16:30:28

Why can't you assign a number with a decimal point to the decimal type directly without using type suffix? isn't this kind of number considered a number of type decimal?

为什么不在不使用类型后缀的情况下直接将带小数点的数字分配给小数类型?是不是这种数字被认为是一些十进制数?

decimal bankBalance = 3433.20; // ERROR!

5 个解决方案

#1


Edit: I may have missed the last part of the question, so the overview below is hardly useful.

编辑:我可能错过了问题的最后部分,因此下面的概述几乎没有用。

Anyway, the reason you can't do what you're trying to do is because there is no implicit conversion between floating point types and decimal. You can however assign it from an integer, as there is an implicit conversion from int to decimal.

无论如何,你不能做你想要做的事情的原因是因为浮点类型和十进制之间没有隐式转换。但是,您可以从整数中分配它,因为存在从int到decimal的隐式转换。


You can, but you have to use this syntax (or do an explicit cast to decimal).

您可以,但必须使用此语法(或对十进制执行显式转换)。

decimal bankBalance = 3433.20m;

and for floats it is

对于花车来说

float bankBalance = 3433.20f;

default is double

默认是双倍

double bankBalance = 3444.20;

#2


Actually, hidden spec feature: you can ;-p

实际上,隐藏的规格功能:你可以;-p

decimal bankBalance = (decimal)3433.20;

This is genuinely parsed by the compiler as a decimal (not a float and a cast). See the IL to prove it. Note that the precision gets truncated, though (this has 1 decimal digit, not the 2 you get from the M version).

这是由编译器真正解析为十进制(不是浮点数和强制转换)。请参阅IL来证明这一点。请注意,精度会被截断(这有1个十进制数字,而不是从M版本获得的2)。

IL generated:

L_0001: ldc.i4 0x861c
L_0006: ldc.i4.0 
L_0007: ldc.i4.0 
L_0008: ldc.i4.0 
L_0009: ldc.i4.1 
L_000a: newobj instance void [mscorlib]System.Decimal::.ctor(int32, int32, int32, bool, uint8)
L_000f: stloc.0 

Compared to:

decimal bankBalance = 3433.20M;

Which generates:

L_0001: ldc.i4 0x53d18
L_0006: ldc.i4.0 
L_0007: ldc.i4.0 
L_0008: ldc.i4.0 
L_0009: ldc.i4.2 
L_000a: newobj instance void [mscorlib]System.Decimal::.ctor(int32, int32, int32, bool, uint8)
L_000f: stloc.0 

The only difference is the decimal digits (1 vs 2, and a factor of 10, accordingly)

唯一的区别是十进制数字(1对2,因此为10)

#3


This

decimal bankBalance = 3433.20M;

will work. The reason is that float and decimal are very different types. float will give you an extremely close approximation of the number you enter, but decimal will give you the exact number. 99% of the time you wont notice the difference, and should just use float.

将工作。原因是浮点数和小数是非常不同的类型。 float会给你一个非常接近你输入数字的近似值,但decimal会给你一个确切的数字。 99%的时间你不会注意到差异,应该只使用浮动。

#4


Your answer consists of two important points:

你的答案包括两个要点:

  1. All numerical literals with a decimal point are inferred to be of type double by the C# compiler, consequently, 3433.20 is a double by default.

    带有小数点的所有数字文字都被C#编译器推断为double类型,因此,3433.20默认为double。

  2. double numbers do not implicitly convert to decimal because although decimal is more precise than double it covers a shorter range so overflow is possible during a cast from double to decimal.

    双数字不会隐式转换为十进制,因为虽然十进制比double更精确,但它覆盖的范围更短,因此在从double到decimal的转换期间可以溢出。

double's range: ±(~10^−324 to 10^308) with 15 or 16 significant figures.

double的范围:±(~10 ^ -324到10 ^ 308),有15或16位有效数字。

decimal's range: ±(~10^-28 to 10^28) with 28 or 29 significant figures.

十进制范围:±(~10 ^ -28到10 ^ 28),有28或29位有效数字。

#5


See the MSDN page on decimal which explains that there is no implicit conversion between normal float types and decimal.

请参阅十进制的MSDN页面,该页面解释了普通浮点类型和十进制之间没有隐式转换。

Try

decimal bankBalance = 3433.20m;

decimal bankBalance = 3433.20m;

#1


Edit: I may have missed the last part of the question, so the overview below is hardly useful.

编辑:我可能错过了问题的最后部分,因此下面的概述几乎没有用。

Anyway, the reason you can't do what you're trying to do is because there is no implicit conversion between floating point types and decimal. You can however assign it from an integer, as there is an implicit conversion from int to decimal.

无论如何,你不能做你想要做的事情的原因是因为浮点类型和十进制之间没有隐式转换。但是,您可以从整数中分配它,因为存在从int到decimal的隐式转换。


You can, but you have to use this syntax (or do an explicit cast to decimal).

您可以,但必须使用此语法(或对十进制执行显式转换)。

decimal bankBalance = 3433.20m;

and for floats it is

对于花车来说

float bankBalance = 3433.20f;

default is double

默认是双倍

double bankBalance = 3444.20;

#2


Actually, hidden spec feature: you can ;-p

实际上,隐藏的规格功能:你可以;-p

decimal bankBalance = (decimal)3433.20;

This is genuinely parsed by the compiler as a decimal (not a float and a cast). See the IL to prove it. Note that the precision gets truncated, though (this has 1 decimal digit, not the 2 you get from the M version).

这是由编译器真正解析为十进制(不是浮点数和强制转换)。请参阅IL来证明这一点。请注意,精度会被截断(这有1个十进制数字,而不是从M版本获得的2)。

IL generated:

L_0001: ldc.i4 0x861c
L_0006: ldc.i4.0 
L_0007: ldc.i4.0 
L_0008: ldc.i4.0 
L_0009: ldc.i4.1 
L_000a: newobj instance void [mscorlib]System.Decimal::.ctor(int32, int32, int32, bool, uint8)
L_000f: stloc.0 

Compared to:

decimal bankBalance = 3433.20M;

Which generates:

L_0001: ldc.i4 0x53d18
L_0006: ldc.i4.0 
L_0007: ldc.i4.0 
L_0008: ldc.i4.0 
L_0009: ldc.i4.2 
L_000a: newobj instance void [mscorlib]System.Decimal::.ctor(int32, int32, int32, bool, uint8)
L_000f: stloc.0 

The only difference is the decimal digits (1 vs 2, and a factor of 10, accordingly)

唯一的区别是十进制数字(1对2,因此为10)

#3


This

decimal bankBalance = 3433.20M;

will work. The reason is that float and decimal are very different types. float will give you an extremely close approximation of the number you enter, but decimal will give you the exact number. 99% of the time you wont notice the difference, and should just use float.

将工作。原因是浮点数和小数是非常不同的类型。 float会给你一个非常接近你输入数字的近似值,但decimal会给你一个确切的数字。 99%的时间你不会注意到差异,应该只使用浮动。

#4


Your answer consists of two important points:

你的答案包括两个要点:

  1. All numerical literals with a decimal point are inferred to be of type double by the C# compiler, consequently, 3433.20 is a double by default.

    带有小数点的所有数字文字都被C#编译器推断为double类型,因此,3433.20默认为double。

  2. double numbers do not implicitly convert to decimal because although decimal is more precise than double it covers a shorter range so overflow is possible during a cast from double to decimal.

    双数字不会隐式转换为十进制,因为虽然十进制比double更精确,但它覆盖的范围更短,因此在从double到decimal的转换期间可以溢出。

double's range: ±(~10^−324 to 10^308) with 15 or 16 significant figures.

double的范围:±(~10 ^ -324到10 ^ 308),有15或16位有效数字。

decimal's range: ±(~10^-28 to 10^28) with 28 or 29 significant figures.

十进制范围:±(~10 ^ -28到10 ^ 28),有28或29位有效数字。

#5


See the MSDN page on decimal which explains that there is no implicit conversion between normal float types and decimal.

请参阅十进制的MSDN页面,该页面解释了普通浮点类型和十进制之间没有隐式转换。

Try

decimal bankBalance = 3433.20m;

decimal bankBalance = 3433.20m;