如何在C#中将数字四舍五入到小数点后两位?

时间:2020-12-25 11:10:37

I want to do this using the Math.Round function

我想使用Math.Round函数执行此操作

13 个解决方案

#1


517  

Here's some examples:

这是一些例子:

decimal a = 1.994444M;

Math.Round(a, 2); //returns 1.99

decimal b = 1.995555M;

Math.Round(b, 2); //returns 2.00

You might also want to look at bankers rounding / round-to-even with the following overload:

你可能还想看看银行家四舍五入/四舍五入以下过载:

Math.Round(a, 2, MidpointRounding.ToEven);

There's more information on it here.

这里有更多相关信息。

#2


79  

Try this:

twoDec = Math.Round(val, 2)

#3


33  

Personally I never round anything. Keep it as resolute as possible, since rounding is a bit of a red herring in CS anyway. But you do want to format data for your users, and to that end, I find that string.Format("{0:0.00}", number) is a good approach.

就个人而言,我永远不会圆。保持尽可能坚定,因为无论如何,舍入在CS中都是一个红色的鲱鱼。但是你确实希望为用户格式化数据,为此,我发现string.Format(“{0:0.00}”,number)是一个很好的方法。

#4


26  

If you'd like a string

如果你想要一个字符串

> (1.7289).ToString("#.##")
"1.73"

Or a decimal

或小数

> Math.Round((Decimal)x, 2)
1.73m

But remember! Rounding is not distributive, ie. round(x*y) != round(x) * round(y). So don't do any rounding until the very end of a calculation, else you'll lose accuracy.

但要记住!舍入不是分配的,即。 round(x * y)!= round(x)* round(y)。因此,在计算结束之前不要进行任何舍入,否则您将失去准确性。

#5


12  

Wikipedia has a nice page on rounding in general.

*有一个关于四舍五入的好页面。

All .NET (managed) languages can use any of the common language run time's (the CLR) rounding mechanisms. For example, the Math.Round() (as mentioned above) method allows the developer to specify the type of rounding (Round-to-even or Away-from-zero). The Convert.ToInt32() method and its variations use round-to-even. The Ceiling() and Floor() methods are related.

所有.NET(托管)语言都可以使用任何公共语言运行时(CLR)舍入机制。例如,Math.Round()(如上所述)方法允许开发人员指定舍入类型(Round-to-even或Away-from-zero)。 Convert.ToInt32()方法及其变体使用round-to-even。 Ceiling()和Floor()方法是相关的。

You can round with custom numeric formatting as well.

您也可以使用自定义数字格式进行舍入。

Note that Decimal.Round() uses a different method than Math.Round();

请注意,Decimal.Round()使用的方法与Math.Round()不同;

Here is a useful post on the banker's rounding algorithm. See one of Raymond's humorous posts here about rounding...

这是关于银行家舍入算法的有用帖子。看看雷蒙德在这里关于四舍五入的幽默帖子之一......

#6


11  

// convert upto two decimal places

//最多转换两位小数

String.Format("{0:0.00}", 140.6767554);        // "140.67"
String.Format("{0:0.00}", 140.1);             // "140.10"
String.Format("{0:0.00}", 140);              // "140.00"

Double d = 140.6767554;
Double dc = Math.Round((Double)d, 2);       //  140.67

decimal d = 140.6767554M;
decimal dc = Math.Round(d, 2);             //  140.67

=========

// just two decimal places
String.Format("{0:0.##}", 123.4567);      // "123.46"
String.Format("{0:0.##}", 123.4);         // "123.4"
String.Format("{0:0.##}", 123.0);         // "123"

can also combine "0" with "#".

也可以将“0”与“#”组合。

String.Format("{0:0.0#}", 123.4567)       // "123.46"
String.Format("{0:0.0#}", 123.4)          // "123.4"
String.Format("{0:0.0#}", 123.0)          // "123.0"

#7


6  

I know its an old question but please note for the following differences between Math round and String format round:

我知道这是一个老问题,但请注意Math round和String format round之间的以下区别:

decimal d1 = (decimal)1.125;
Math.Round(d1, 2).Dump();   // returns 1.12
d1.ToString("#.##").Dump(); // returns "1.13"

decimal d2 = (decimal)1.1251;
Math.Round(d2, 2).Dump();   // returns 1.13
d2.ToString("#.##").Dump(); // returns "1.13"

#8


5  

This is for rounding to 2 decimal places in C#:

这是用于在C#中舍入到2个小数位:

label8.Text = valor_cuota .ToString("N2") ;

In VB.NET:

 Imports System.Math
 round(label8.text,2)

#9


4  

One thing you may want to check is the Rounding Mechanism of Math.Round:

您可能想要检查的一件事是Math.Round的Rounding Mechanism:

http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx

Other than that, I recommend the Math.Round(inputNumer, numberOfPlaces) approach over the *100/100 one because it's cleaner.

除此之外,我推荐Math.Round(inputNumer,numberOfPlaces)方法超过* 100/100,因为它更干净。

#10


3  

You should be able to specify the number of digits you want to round to using Math.Round(YourNumber, 2)

您应该可以使用Math.Round(YourNumber,2)指定要舍入的位数

You can read more here.

你可以在这里阅读更多。

#11


1  

string a = "10.65678";

string a =“10.65678”;

decimal d = Math.Round(Convert.ToDouble(a.ToString()),2)

decimal d = Math.Round(Convert.ToDouble(a.ToString()),2)

#12


0  

  public double RoundDown(double number, int decimalPlaces)
        {
            return Math.Floor(number * Math.Pow(10, decimalPlaces)) / Math.Pow(10, decimalPlaces);
        }

#13


0  

Math.Floor(123456.646 * 100) / 100 Would return 123456.64

Math.Floor(123456.646 * 100)/ 100将返回123456.64

#1


517  

Here's some examples:

这是一些例子:

decimal a = 1.994444M;

Math.Round(a, 2); //returns 1.99

decimal b = 1.995555M;

Math.Round(b, 2); //returns 2.00

You might also want to look at bankers rounding / round-to-even with the following overload:

你可能还想看看银行家四舍五入/四舍五入以下过载:

Math.Round(a, 2, MidpointRounding.ToEven);

There's more information on it here.

这里有更多相关信息。

#2


79  

Try this:

twoDec = Math.Round(val, 2)

#3


33  

Personally I never round anything. Keep it as resolute as possible, since rounding is a bit of a red herring in CS anyway. But you do want to format data for your users, and to that end, I find that string.Format("{0:0.00}", number) is a good approach.

就个人而言,我永远不会圆。保持尽可能坚定,因为无论如何,舍入在CS中都是一个红色的鲱鱼。但是你确实希望为用户格式化数据,为此,我发现string.Format(“{0:0.00}”,number)是一个很好的方法。

#4


26  

If you'd like a string

如果你想要一个字符串

> (1.7289).ToString("#.##")
"1.73"

Or a decimal

或小数

> Math.Round((Decimal)x, 2)
1.73m

But remember! Rounding is not distributive, ie. round(x*y) != round(x) * round(y). So don't do any rounding until the very end of a calculation, else you'll lose accuracy.

但要记住!舍入不是分配的,即。 round(x * y)!= round(x)* round(y)。因此,在计算结束之前不要进行任何舍入,否则您将失去准确性。

#5


12  

Wikipedia has a nice page on rounding in general.

*有一个关于四舍五入的好页面。

All .NET (managed) languages can use any of the common language run time's (the CLR) rounding mechanisms. For example, the Math.Round() (as mentioned above) method allows the developer to specify the type of rounding (Round-to-even or Away-from-zero). The Convert.ToInt32() method and its variations use round-to-even. The Ceiling() and Floor() methods are related.

所有.NET(托管)语言都可以使用任何公共语言运行时(CLR)舍入机制。例如,Math.Round()(如上所述)方法允许开发人员指定舍入类型(Round-to-even或Away-from-zero)。 Convert.ToInt32()方法及其变体使用round-to-even。 Ceiling()和Floor()方法是相关的。

You can round with custom numeric formatting as well.

您也可以使用自定义数字格式进行舍入。

Note that Decimal.Round() uses a different method than Math.Round();

请注意,Decimal.Round()使用的方法与Math.Round()不同;

Here is a useful post on the banker's rounding algorithm. See one of Raymond's humorous posts here about rounding...

这是关于银行家舍入算法的有用帖子。看看雷蒙德在这里关于四舍五入的幽默帖子之一......

#6


11  

// convert upto two decimal places

//最多转换两位小数

String.Format("{0:0.00}", 140.6767554);        // "140.67"
String.Format("{0:0.00}", 140.1);             // "140.10"
String.Format("{0:0.00}", 140);              // "140.00"

Double d = 140.6767554;
Double dc = Math.Round((Double)d, 2);       //  140.67

decimal d = 140.6767554M;
decimal dc = Math.Round(d, 2);             //  140.67

=========

// just two decimal places
String.Format("{0:0.##}", 123.4567);      // "123.46"
String.Format("{0:0.##}", 123.4);         // "123.4"
String.Format("{0:0.##}", 123.0);         // "123"

can also combine "0" with "#".

也可以将“0”与“#”组合。

String.Format("{0:0.0#}", 123.4567)       // "123.46"
String.Format("{0:0.0#}", 123.4)          // "123.4"
String.Format("{0:0.0#}", 123.0)          // "123.0"

#7


6  

I know its an old question but please note for the following differences between Math round and String format round:

我知道这是一个老问题,但请注意Math round和String format round之间的以下区别:

decimal d1 = (decimal)1.125;
Math.Round(d1, 2).Dump();   // returns 1.12
d1.ToString("#.##").Dump(); // returns "1.13"

decimal d2 = (decimal)1.1251;
Math.Round(d2, 2).Dump();   // returns 1.13
d2.ToString("#.##").Dump(); // returns "1.13"

#8


5  

This is for rounding to 2 decimal places in C#:

这是用于在C#中舍入到2个小数位:

label8.Text = valor_cuota .ToString("N2") ;

In VB.NET:

 Imports System.Math
 round(label8.text,2)

#9


4  

One thing you may want to check is the Rounding Mechanism of Math.Round:

您可能想要检查的一件事是Math.Round的Rounding Mechanism:

http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx

Other than that, I recommend the Math.Round(inputNumer, numberOfPlaces) approach over the *100/100 one because it's cleaner.

除此之外,我推荐Math.Round(inputNumer,numberOfPlaces)方法超过* 100/100,因为它更干净。

#10


3  

You should be able to specify the number of digits you want to round to using Math.Round(YourNumber, 2)

您应该可以使用Math.Round(YourNumber,2)指定要舍入的位数

You can read more here.

你可以在这里阅读更多。

#11


1  

string a = "10.65678";

string a =“10.65678”;

decimal d = Math.Round(Convert.ToDouble(a.ToString()),2)

decimal d = Math.Round(Convert.ToDouble(a.ToString()),2)

#12


0  

  public double RoundDown(double number, int decimalPlaces)
        {
            return Math.Floor(number * Math.Pow(10, decimalPlaces)) / Math.Pow(10, decimalPlaces);
        }

#13


0  

Math.Floor(123456.646 * 100) / 100 Would return 123456.64

Math.Floor(123456.646 * 100)/ 100将返回123456.64