为什么不使用类型后缀就不能直接给十进制类型分配一个带小数点的数字?

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!

#0

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.


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;

#1

Actually, hidden spec feature: you can ;-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 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)

#2

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.

#3

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.

  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's range: ±(~10^−324 to 10^308) with 15 or 16 significant figures.

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

#4

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

Try

decimal bankBalance = 3433.20m;

推荐文章

部分降价分析

部分降价分析

推荐文章

如何在Python中创建3d对象/类?

如何在Python中创建3d对象/类?

推荐文章

NHibernate3.2代码映射。当我们创建包关系时,什么意思一人公寓?

NHibernate3.2代码映射。当我们创建包关系时,什么意思一人公寓?

推荐文章

迭代二维向量

迭代二维向量

推荐文章

如何在bash中连接来自同一段落的行?

如何在bash中连接来自同一段落的行?

推荐文章

如何在Windows Photo Viewer/Picasa Viewer中添加对专有图像格式的支持

如何在Windows Photo Viewer/Picasa Viewer中添加对专有图像格式的支持

推荐文章

项目导航器中的xcode文件标记框

项目导航器中的xcode文件标记框

推荐文章

这个查询如何转换为动态Linq表达式?

这个查询如何转换为动态Linq表达式?

推荐文章

命令中的git-dot表示法是更通用的一部分吗?

命令中的git-dot表示法是更通用的一部分吗?

推荐文章

delphi中的补丁程序调用

delphi中的补丁程序调用

推荐文章

Java并发-写入同一数组的不同索引

Java并发-写入同一数组的不同索引

推荐文章

通过jquery ajax发送json无效

通过jquery ajax发送json无效

推荐文章

(Cocos2D)检测正在显示哪个CCScene?

(Cocos2D)检测正在显示哪个CCScene?

推荐文章

无法将JAX-RS服务连接到MVC模板

无法将JAX-RS服务连接到MVC模板

推荐文章

水-browser.select_列表不保留动态内容

水-browser.select_列表不保留动态内容

推荐文章

运算符<<重载

运算符