类型:QUOT的有趣的行为;十进制"在C#中 [英] Interesting behaviour of type "decimal" in C#
本文介绍了类型:QUOT的有趣的行为;十进制"在C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我们声明填充为const小数,填充是行不通的。
mymoney = 1.2和金钱= 1.20,怎么能这种行为解释呢?
类节目
{
静态无效的主要(字串[] args)
{
十进制余额= 1.2米
常量十进制ConstPadding =0.00米;
十进制填充=0.00米;
十进制mymoney = decimal.Round(余额+ ConstPadding,2);
十进制yourmoney = decimal.Round(余额+填充,2);
Console.WriteLine(mymoney); // 1.2
Console.WriteLine(yourmoney); //1.20
}
}
解决方案
作为伴奏乔恩的回答,下面是IL从您的代码产生的。正如他所说,mymoney从未被添加。
。方法私人hidebysig静态无效的主要(字串[] args)CIL管理
{
.entrypoint
//代码大小61(0x3D之间)
.maxstack 6
.locals的init([0]值类型[mscorlib程序] System.Decimal平衡,
[1]的ValueType [mscorlib程序] System.Decimal填充,
[2]的ValueType [mscorlib程序] System.Decimal mymoney,
[3]的ValueType [mscorlib程序] System.Decimal yourmoney)
IL_0000:NOP
IL_0001:ldc.i4.s 12
IL_0003:ldc.i4.0
IL_0004:ldc.i4.0
IL_0005:ldc.i4.0
IL_0006:ldc.i4.1
IL_0007:newobj实例无效[mscorlib程序] System.Decimal ::构造函数(INT32,
INT32,
INT32,
BOOL,
UINT8)
IL_000c:stloc.0
IL_000d:ldc.i4.0
IL_000e:ldc.i4.0
IL_000f:ldc.i4.0
IL_0010:ldc.i4.0
IL_0011:ldc.i4.2
IL_0012:newobj实例无效[mscorlib程序] System.Decimal ::构造函数(INT32,
INT32,
INT32,
布尔,
UINT8)
IL_0017:stloc.1
IL_0018:ldloc.0
IL_0019:ldc.i4.2
IL_001a:调用值类型[mscorlib程序] System.Decimal [mscorlib程序] System.Decimal :: ROUND(值类型[mscorlib程序] System.Decimal,
INT32)
IL_001f:stloc.2
IL_0020:ldloc.0
IL_0021:ldloc.1
IL_0022:调用值类型[mscorlib程序] System.Decimal [mscorlib程序] System.Decimal :: op_Addition(值类型[mscorlib程序] System.Decimal,
值类型[mscorlib程序]系统.Decimal)
IL_0027:ldc.i4.2
IL_0028:调用值类型[mscorlib程序] System.Decimal [mscorlib程序] System.Decimal :: ROUND(值类型[mscorlib程序] System.Decimal,
INT32)
IL_002d:stloc.3
IL_002e:ldloc.2
IL_002f:呼叫无效[mscorlib程序] System.Console:的WriteLine(值类型[mscorlib程序] System.Decimal)
IL_0034:NOP
IL_0035:ldloc.3
IL_0036:呼叫无效[mscorlib程序] System.Console:的WriteLine(值类型[mscorlib程序] System.Decimal)
IL_003b:NOP
IL_003c :RET
} //方法程序::主要
要产生IL(结束即如果你想在未来的罩),刚刚从VS命令提示符下运行ILDASM下看,然后加载您的可执行文件,并在您想看看在方法上双击。
If we declare padding as const decimal, the padding is not working.
mymoney = 1.2 and your money = 1.20, how can this behavior be explained?
class Program
{
static void Main(string[] args)
{
decimal balance = 1.2m;
const decimal ConstPadding = 0.00m;
decimal padding = 0.00m;
decimal mymoney = decimal.Round(balance + ConstPadding, 2);
decimal yourmoney = decimal.Round(balance + padding, 2);
Console.WriteLine(mymoney); // 1.2
Console.WriteLine(yourmoney); //1.20
}
}
解决方案
As an accompaniment to Jon's answer, below is the IL produced from your code. As he mentioned, mymoney was never added.
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 61 (0x3d)
.maxstack 6
.locals init ([0] valuetype [mscorlib]System.Decimal balance,
[1] valuetype [mscorlib]System.Decimal padding,
[2] valuetype [mscorlib]System.Decimal mymoney,
[3] valuetype [mscorlib]System.Decimal yourmoney)
IL_0000: nop
IL_0001: ldc.i4.s 12
IL_0003: ldc.i4.0
IL_0004: ldc.i4.0
IL_0005: ldc.i4.0
IL_0006: ldc.i4.1
IL_0007: newobj instance void [mscorlib]System.Decimal::.ctor(int32,
int32,
int32,
bool,
uint8)
IL_000c: stloc.0
IL_000d: ldc.i4.0
IL_000e: ldc.i4.0
IL_000f: ldc.i4.0
IL_0010: ldc.i4.0
IL_0011: ldc.i4.2
IL_0012: newobj instance void [mscorlib]System.Decimal::.ctor(int32,
int32,
int32,
bool,
uint8)
IL_0017: stloc.1
IL_0018: ldloc.0
IL_0019: ldc.i4.2
IL_001a: call valuetype [mscorlib]System.Decimal [mscorlib]System.Decimal::Round(valuetype [mscorlib]System.Decimal,
int32)
IL_001f: stloc.2
IL_0020: ldloc.0
IL_0021: ldloc.1
IL_0022: call valuetype [mscorlib]System.Decimal [mscorlib]System.Decimal::op_Addition(valuetype [mscorlib]System.Decimal,
valuetype [mscorlib]System.Decimal)
IL_0027: ldc.i4.2
IL_0028: call valuetype [mscorlib]System.Decimal [mscorlib]System.Decimal::Round(valuetype [mscorlib]System.Decimal,
int32)
IL_002d: stloc.3
IL_002e: ldloc.2
IL_002f: call void [mscorlib]System.Console::WriteLine(valuetype [mscorlib]System.Decimal)
IL_0034: nop
IL_0035: ldloc.3
IL_0036: call void [mscorlib]System.Console::WriteLine(valuetype [mscorlib]System.Decimal)
IL_003b: nop
IL_003c: ret
} // end of method Program::Main
To produce the IL (i.e. if you want to look under the hood in the future), just run ILDASM from a VS command prompt, then load your executable and double-click on the method that you would like to look at.
这篇关于类型:QUOT的有趣的行为;十进制"在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文