C# 点后只保留两位小数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1291483/
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
Leave only two decimal places after the dot
提问by Sergio Tapia
public void LoadAveragePingTime()
{
try
{
PingReply pingReply = pingClass.Send("logon.chronic-domination.com");
double AveragePing = (pingReply.RoundtripTime / 1.75);
label4.Text = (AveragePing.ToString() + "ms");
}
catch (Exception)
{
label4.Text = "Server is currently offline.";
}
}
Currently my label4.Text get's something like: "187.371698712637".
目前我的 label4.Text 是这样的:“187.371698712637”。
I need it to show something like: "187.37"
我需要它显示如下内容:“187.37”
Only two posts after the DOT. Can someone help me out?
DOT 之后只有两个职位。有人可以帮我吗?
采纳答案by Matt Grande
string.Format
is your friend.
string.Format
是你的朋友。
String.Format("{0:0.00}", 123.4567); // "123.46"
回答by Steven Sudit
// just two decimal places
String.Format("{0:0.00}", 123.4567); // "123.46"
String.Format("{0:0.00}", 123.4); // "123.40"
String.Format("{0:0.00}", 123.0); // "123.00"
http://www.csharp-examples.net/string-format-double/
http://www.csharp-examples.net/string-format-double/
edit
编辑
No idea why they used "String" instead of "string", but the rest is correct.
不知道为什么他们使用“字符串”而不是“字符串”,但其余的都是正确的。
回答by Anas
If you want to take just two numbers after comma you can use the Math Class that give you the round function for example :
如果您只想在逗号后取两个数字,您可以使用为您提供舍入函数的数学类,例如:
float value = 92.197354542F;
value = (float)System.Math.Round(value,2); // value = 92.2;
Hope this Help
Cheers
希望这有助于
干杯
回答by Miru
double amount = 31.245678;
amount = Math.Floor(amount * 100) / 100;
回答by Taylor Flatt
Alternatively, you may also use the composite operator F then indicating how many decimal spots you wish to appear after the decimal.
或者,您也可以使用复合运算符 F 然后指示您希望在小数点后出现多少个小数点。
string.Format("{0:F2}", 123.456789); //123.46
string.Format("{0:F3}", 123.456789); //123.457
string.Format("{0:F4}", 123.456789); //123.4568
It will round up so be aware of that.
它将四舍五入,因此请注意这一点。
I sourced the general documentation. There are a ton of other formatting operators there as well that you may check out.
我获取了一般文档。那里还有大量其他格式化运算符,您可以查看。
Source: https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx
来源:https: //msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx
回答by Himanshu Shukla
You can use this
你可以用这个
"String.Format("{0:F2}", String Value);"
"String.Format("{0:F2}", 字符串值);"
Gives you only the two digits after Dot, exactly two digits.
只给你 Dot 之后的两位数,正好是两位数。
回答by Tanmay Nehete
Try This
尝试这个
public static string PreciseDecimalValue(double Value, int DigitsAfterDecimal)
{
string PreciseDecimalFormat = "{0:0.0}";
for (int count = 2; count <= DigitsAfterDecimal; count++)
{
PreciseDecimalFormat = PreciseDecimalFormat.Insert(PreciseDecimalFormat.LastIndexOf('}'), "0");
}
return String.Format(PreciseDecimalFormat, Value);
}
回答by Md. Shafiqur Rahman
Using the property of String
使用属性 String
double value = 123.456789;
String.Format("{0:0.00}", value);
Note: This can be used to display only.
注意:这只能用于显示。
Using System.Math
使用 System.Math
double value = 123.456789;
System.Math.Round(value, 2);
回答by emanuel
Use string interpolation decimalVar:0.00
使用字符串插值 decimalVar:0.00
回答by user3077282
Try this:
尝试这个:
double result = Math.Round(24.576938593,2);
MessageBox.Show(result.ToString());
Output: 24.57
输出:24.57