Leave only two decimal places after the dot

Sergio Tapia picture Sergio Tapia · Aug 18, 2009 · Viewed 209.7k times · Source
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?

Answer

Matt Grande picture Matt Grande · Aug 18, 2009

string.Format is your friend.

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