在 C# 中如何将数字四舍五入到两位小数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/257005/
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
How do you round a number to two decimal places in C#?
提问by
I want to do this using the Math.Round
function
我想用这个Math.Round
函数来做到这一点
回答by John Boker
Try this:
尝试这个:
twoDec = Math.Round(val, 2)
回答by Eoin Campbell
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.
有一个关于它的更多信息在这里。
回答by Kevin W Lee
回答by Michael Stum
One thing you may want to check is the Rounding Mechanism of Math.Round:
您可能想要检查的一件事是 Math.Round 的舍入机制:
http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx
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 方法,因为它更干净。
回答by Foredecker
Wikipedia has a nice pageon 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。该天花板()和地面()方法是相关的。
You can round with custom numeric formattingas 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 hereabout rounding...
回答by sadim
This is for rounding to 2 decimal places in C#:
这是为了在 C# 中四舍五入到小数点后两位:
label8.Text = valor_cuota .ToString("N2") ;
In VB.NET:
在 VB.NET 中:
Imports System.Math
round(label8.text,2)
回答by Gleno
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)
是一个很好的方法。
回答by Colonel Panic
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)
. 因此,在计算结束之前不要进行任何四舍五入,否则您将失去准确性。
回答by Abhishek Jaiswal
string a = "10.65678";
字符串 a = "10.65678";
decimal d = Math.Round(Convert.ToDouble(a.ToString()),2)
十进制 d = Math.Round(Convert.ToDouble(a.ToString()),2)
回答by Rae Lee
// 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"