.NET 默认十进制格式

.NET default formatting of decimal

为什么这两个代码有不同的行为?

decimal test = 53M;
var label = "Some thing " + test + " other thing";
Console.WriteLine(label);

test = 53.00M;
label = "Some thing " + test + " other thing";
Console.WriteLine(label);

显示:

Some thing 53 other thing

Some thing 53,00 other thing

如果我们参考Decimal

二进制表示

https://docs.microsoft.com/en-us/dotnet/api/system.decimal.getbits?view=netframework-4.8

The binary representation of a Decimal number consists of a 1-bit sign, a 96-bit integer number, and a scaling factor used to divide the integer number and specify what portion of it is a decimal fraction. The scaling factor is implicitly the number 10, raised to an exponent ranging from 0 to 28.

粗体 是我的,Dmitry Bychenko)

我们可以很容易地解释 53M53.00M 之间的 差异 :

 53M     == {Integer Number:   53; Scaling Factor: 0} ==   53 / 10**0 
 53.00M  == {Integer Number: 5300; Scaling Factor: 2} == 5300 / 10**2