C# 的固定点类型

decimal 类型的范围较小,但精度高于 double。 “固定点”一词意味着,十进制小数点(或二进制小数点)不会移动。

decimal min = decimal.MinValue;
decimal max = decimal.MaxValue;
Console.WriteLine($"The range of the decimal type is {min} to {max}");

输出

The range of the decimal type is -79228162514264337593543950335 to 79228162514264337593543950335

十进制类型的精度更高: 

double a = 1.0;
double b = 3.0;
Console.WriteLine(a / b);

decimal c = 1.0M;
decimal d = 3.0M;
Console.WriteLine(c / d);

输出

0.333333333333333
0.3333333333333333333333333333

数字中的 M 后缀指明了常数应如何使用 decimal 类型。

可以看到,使用十进制类型执行数学运算时,十进制小数点右侧的数字更多。

double r = 2.50;
Console.WriteLine(r * r * Math.PI);

输出

19.6349540849362
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值