调整小数precision,.NET [英] Adjusting decimal precision, .net

查看:166
本文介绍了调整小数precision,.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中这些行

decimal a = 2m;
decimal b = 2.0m;
decimal c = 2.00000000m;
decimal d = 2.000000000000000000000000000m;

Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.WriteLine(d);

生成的输出:

2
2.0
2.00000000
2.000000000000000000000000000

所以,我可以看到,从文字创建一个小数变量可以让我控制precision。

So I can see that creating a decimal variable from a literal allows me to control the precision.

  • 我可以调整小数变量precision不使用文字?
  • 如何从创建B'我怎样才能从C创建B'

推荐答案

preserving尾随零像这样被引入了更加严格的一致性.NET 1.1与ECMA CLI规范。

Preserving trailing zeroes like this was introduced in .NET 1.1 for more strict conformance with the ECMA CLI specification.

有这方面的一些信息MSDN上,例如这里

There is some info on this on MSDN, e.g. here.

您可以按如下调整precision:

You can adjust the precision as follows:

  • Math.Round(或天花板,地板等),以降低(从C B)precision

  • Math.Round (or Ceiling, Floor etc) to decrease precision (b from c)

乘以1.000 ...(你想要的小数位数)增加precision - 如:通过1.0M乘从拿到湾

Multiply by 1.000... (with the number of decimals you want) to increase precision - e.g. multiply by 1.0M to get b from a.

这篇关于调整小数precision,.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆