点后仅保留两位小数

发布于 2024-08-01 16:12:13 字数 497 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

长亭外,古道边 2024-08-08 16:12:13

使用String的属性

double value = 123.456789;
String.Format("{0:0.00}", value);

注意:这只能用于显示。

使用System.Math

double value = 123.456789;
System.Math.Round(value, 2);

Using the property of String

double value = 123.456789;
String.Format("{0:0.00}", value);

Note: This can be used to display only.

Using System.Math

double value = 123.456789;
System.Math.Round(value, 2);
单身狗的梦 2024-08-08 16:12:13

使用字符串插值decimalVar:0.00

Use string interpolation decimalVar:0.00

人事已非 2024-08-08 16:12:13

尝试这个

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);
        }

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);
        }
つ低調成傷 2024-08-08 16:12:13

string.Format 是你的朋友。

String.Format("{0:0.00}", 123.4567);      // "123.46"

string.Format is your friend.

String.Format("{0:0.00}", 123.4567);      // "123.46"
表情可笑 2024-08-08 16:12:13

如果您只想在逗号后取两个数字,您可以使用为您提供舍入函数的数学类,例如:

float value = 92.197354542F;
value = (float)System.Math.Round(value,2);         // value = 92.2;

希望有帮助
干杯

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

稚然 2024-08-08 16:12:13
// 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/

编辑

不知道为什么他们使用“String”而不是“string”,但剩下的就是正确的。

// 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/

edit

No idea why they used "String" instead of "string", but the rest is correct.

阳光下慵懒的猫 2024-08-08 16:12:13
yourValue.ToString("0.00") will work.
yourValue.ToString("0.00") will work.
飘逸的'云 2024-08-08 16:12:13
double amount = 31.245678;
amount = Math.Floor(amount * 100) / 100;
double amount = 31.245678;
amount = Math.Floor(amount * 100) / 100;
一场信仰旅途 2024-08-08 16:12:13

你可以使用这个

"String.Format("{0:F2}", 字符串值);"

仅提供点后的两位数字,恰好是两位数字。

You can use this

"String.Format("{0:F2}", String Value);"

Gives you only the two digits after Dot, exactly two digits.

晌融 2024-08-08 16:12:13

试试这个:

double result = Math.Round(24.576938593,2);
MessageBox.Show(result.ToString());

输出:24.57

Try this:

double result = Math.Round(24.576938593,2);
MessageBox.Show(result.ToString());

Output: 24.57

冰雪梦之恋 2024-08-08 16:12:13

简单的解决方案:

double totalCost = 123.45678;
totalCost = Convert.ToDouble(String.Format("{0:0.00}", totalCost));

//output: 123.45

Simple solution:

double totalCost = 123.45678;
totalCost = Convert.ToDouble(String.Format("{0:0.00}", totalCost));

//output: 123.45
蓝色星空 2024-08-08 16:12:13

double doublVal = 123.45678;

有两种方法。

  1. 以字符串形式显示:

    String.Format("{0:0.00}", doublVal ); 
      
  2. 用于再次获取 Double

    doublVal = Convert.ToDouble(String.Format("{0:0.00}", doublVal )); 
      

double doublVal = 123.45678;

There are two ways.

  1. for display in string:

    String.Format("{0:0.00}", doublVal );
    
  2. for geting again Double

    doublVal = Convert.ToDouble(String.Format("{0:0.00}", doublVal ));
    
逆流 2024-08-08 16:12:13

或者,您也可以使用复合运算符 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

它会四舍五入,所以要注意这一点。

我获取了一般文档。 还有大量其他格式操作符可供您查看。

来源: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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文