Leave only two decimal places after the dot

C#PingDecimal Point

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

string.Format is your friend.

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

Solution 2 - C#

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

Solution 3 - C#

// 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/][1]

edit

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

[1]: http://www.csharp-examples.net/string-format-double/ "http://www.csharp-examples.net/string-format-double/"

Solution 4 - C#

yourValue.ToString("0.00") will work.

Solution 5 - C#

double amount = 31.245678;
amount = Math.Floor(amount * 100) / 100;

Solution 6 - C#

You can use this

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

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

Solution 7 - C#

Try this:

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

Output: 24.57

Solution 8 - C#

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

Solution 9 - C#

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

Solution 10 - C#

Simple solution:

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

//output: 123.45

Solution 11 - C#

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

Solution 12 - C#

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

Solution 13 - C#

Use string interpolation decimalVar:0.00

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSergio TapiaView Question on Stackoverflow
Solution 1 - C#Matt GrandeView Answer on Stackoverflow
Solution 2 - C#AnasView Answer on Stackoverflow
Solution 3 - C#Steven SuditView Answer on Stackoverflow
Solution 4 - C#Sumit JoshiView Answer on Stackoverflow
Solution 5 - C#MiruView Answer on Stackoverflow
Solution 6 - C#Himanshu ShuklaView Answer on Stackoverflow
Solution 7 - C#user3077282View Answer on Stackoverflow
Solution 8 - C#Taylor FlattView Answer on Stackoverflow
Solution 9 - C#Md. Shafiqur RahmanView Answer on Stackoverflow
Solution 10 - C#SaifView Answer on Stackoverflow
Solution 11 - C#Ali RazaView Answer on Stackoverflow
Solution 12 - C#Tanmay NeheteView Answer on Stackoverflow
Solution 13 - C#emanuelView Answer on Stackoverflow