将变量四舍五入到两位小数 C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12621640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:54:55  来源:igfitidea点击:

Rounding a variable to two decimal places C#

c#variablesdecimalrounding

提问by Kerry G

I am interested in how to round variables to two decimal places. In the example below, the bonus is usually a number with four decimal places. Is there any way to ensure the pay variable is always rounded to two decimal places?

我对如何将变量四舍五入到小数点后两位感兴趣。在下面的例子中,奖金通常是一个有四位小数的数字。有什么方法可以确保薪酬变量总是四舍五入到小数点后两位?

  pay = 200 + bonus;
  Console.WriteLine(pay);

采纳答案by Habib

Use Math.Roundand specify the number of decimal places.

使用Math.Round并指定小数位数。

Math.Round(pay,2);

Math.Round Method (Double, Int32)

Math.Round 方法 (Double, Int32)

Rounds a double-precision floating-pointvalue to a specified number of fractional digits.

双精度浮点值舍入到指定的小数位数。

Or Math.Round Method (Decimal, Int32)

Math.Round 方法(十进制,Int32)

Rounds a decimal value to a specified number of fractional digits.

将十进制值舍入到指定的小数位数。

回答by Aghilas Yakoub

decimal pay  = 1.994444M;

Math.Round(pay , 2); 

回答by Jakub Szu?akiewicz

Console.WriteLine(decimal.Round(pay,2));

回答by Paul Aldred-Bann

You can round the result and use string.Formatto set the precision like this:

您可以将结果四舍五入并用于string.Format设置精度,如下所示:

decimal pay = 200.5555m;
pay = Math.Round(pay + bonus, 2);
string payAsString = string.Format("{0:0.00}", pay);

回答by Furqan Safdar

Use System.Math.Round to rounds a decimal value to a specified number of fractional digits.

使用 System.Math.Round 将十进制值四舍五入到指定的小数位数。

var pay = 200 + bonus;
pay = System.Math.Round(pay, 2);
Console.WriteLine(pay);

MSDN References:

MSDN 参考资料:

回答by Sean

Make sure you provide a number, typically a double is used. Math.Round can take 1-3 arguments, the first argument is the variable you wish to round, the second is the number of decimal places and the third is the type of rounding.

确保您提供一个数字,通常使用双精度。Math.Round 可以接受 1-3 个参数,第一个参数是您要四舍五入的变量,第二个是小数位数,第三个是四舍五入的类型。

double pay = 200 + bonus;
double pay = Math.Round(pay);
// Rounds to nearest even number, rounding 0.5 will round "down" to zero because zero is even
double pay = Math.Round(pay, 2, MidpointRounding.ToEven);
// Rounds up to nearest number
double pay = Math.Round(pay, 2, MidpointRounding.AwayFromZero);

回答by Jon Senchyna

You should use a form of Math.Round. Be aware that Math.Rounddefaults to banker's rounding (rounding to the nearest even number) unless you specify a MidpointRoundingvalue. If you don't want to use banker's rounding, you should use Math.Round(decimal d, int decimals, MidpointRounding mode), like so:

您应该使用Math.Round. 请注意,Math.Round除非您指定一个MidpointRounding值,否则默认为银行家四舍五入(四舍五入到最接近的偶数)。如果您不想使用银行家的舍入,则应使用Math.Round(decimal d, int decimals, MidpointRounding mode),如下所示:

Math.Round(pay, 2, MidpointRounding.AwayFromZero); // .005 rounds up to 0.01
Math.Round(pay, 2, MidpointRounding.ToEven);       // .005 rounds to nearest even (0.00) 
Math.Round(pay, 2);    // Defaults to MidpointRounding.ToEven

(Why does .NET use banker's rounding?)

为什么 .NET 使用银行家的舍入?

回答by Tigran

Pay attention on fact that Roundrounds.

注意Round圆的事实。

So (I don't know if it matters in your industry or not), but:

所以(我不知道这对你的行业是否重要),但是:

float a = 12.345f;
Math.Round(a,2);

//result:12,35, and NOT 12.34 !

To make it more precise for your casewe can do something like this:

为了使您的情况更精确,我们可以执行以下操作:

int aInt = (int)(a*100);
float aFloat= aInt /100.0f;
//result:12,34