C# 为什么不能直接将带小数点的数字赋值给decimal类型而不使用类型后缀?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/485185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 05:15:05  来源:igfitidea点击:

why can't you assign a number with a decimal point to decimal type directly without using type suffix?

c#

提问by prodev42

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类型而不使用类型后缀?这种数字不是被认为是小数类型的数字吗?

decimal bankBalance = 3433.20; // ERROR!

采纳答案by Brian Rasmussen

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.

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



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;

回答by Matt Briggs

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 是非常不同的类型。float 会给你一个非常接近你输入的数字的近似值,但 decimal 会给你确切的数字。99% 的情况下您不会注意到差异,应该只使用浮动。

回答by Eddie

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

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

Try

尝试

decimal bankBalance = 3433.20m;

decimal bankBalance = 3433.20m;

回答by Marc Gravell

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 Mversion).

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

IL generated:

IL 生成:

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 的因数)

回答by Saeb Amini

Your answer consists of two important points:

您的回答包含两个要点:

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

  2. doublenumbers do not implicitly convert to decimalbecause although decimalis more precise than doubleit covers a shorter rangeso overflow is possible during a cast from double to decimal.

  1. 所有带小数点的数字文字都被doubleC# 编译器推断为类型,因此,默认情况下3433.20是 a double

  2. double数字不会隐式转换为,decimal因为虽然decimaldouble它涵盖的范围更精确,但在从双精度转换为十进制的转换过程中可能会溢出。

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

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

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

decimal的范围:±(~10^-28 to 10^28)28 或 29 位有效数字。