点后仅保留两位小数
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.";
}
}
目前我的 label4.Text 得到的东西是这样的:“187.371698712637”。
我需要它显示类似:“187.37”
在点之后只有两个帖子。 有人可以帮我吗?
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".
I need it to show something like: "187.37"
Only two posts after the DOT. Can someone help me out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
使用
String
的属性注意:这只能用于显示。
使用System.Math
Using the property of
String
Note: This can be used to display only.
Using
System.Math
使用字符串插值
decimalVar:0.00
Use string interpolation
decimalVar:0.00
尝试这个
Try This
string.Format
是你的朋友。string.Format
is your friend.如果您只想在逗号后取两个数字,您可以使用为您提供舍入函数的数学类,例如:
希望有帮助
干杯
If you want to take just two numbers after comma you can use the Math Class that give you the round function for example :
Hope this Help
Cheers
http://www.csharp-examples.net/string-format-double/
编辑
不知道为什么他们使用“String”而不是“string”,但剩下的就是正确的。
http://www.csharp-examples.net/string-format-double/
edit
No idea why they used "String" instead of "string", but the rest is correct.
你可以使用这个
仅提供点后的两位数字,恰好是两位数字。
You can use this
Gives you only the two digits after Dot, exactly two digits.
试试这个:
输出:24.57
Try this:
Output: 24.57
简单的解决方案:
Simple solution:
double doublVal = 123.45678;
有两种方法。
以字符串形式显示:
用于再次获取 Double
double doublVal = 123.45678;
There are two ways.
for display in string:
for geting again Double
或者,您也可以使用复合运算符 F,然后指示您希望在小数点后出现多少位小数。
它会四舍五入,所以要注意这一点。
我获取了一般文档。 还有大量其他格式操作符可供您查看。
来源:https://msdn.microsoft.com/ en-us/library/dwhawy9k(v=vs.110).aspx
Alternatively, you may also use the composite operator F then indicating how many decimal spots you wish to appear after the decimal.
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