Laisser deux décimales après le point
-
18-09-2019 - |
Question
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.";
}
}
Actuellement, mon label4.Text de quelque chose de get comme:. "187,371698712637"
Je besoin de montrer quelque chose comme: « 187,37 »
Seuls deux postes après le ministère des Transports. Quelqu'un peut-il me aider?
La solution
string.Format
est votre ami.
String.Format("{0:0.00}", 123.4567); // "123.46"
Autres conseils
Si vous voulez prendre seulement deux chiffres après la virgule, vous pouvez utiliser la classe Math qui vous donnent la fonction tour par exemple:
float value = 92.197354542F;
value = (float)System.Math.Round(value,2); // value = 92.2;
Hope this Aide
Vive
// 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/
modifier
Aucune idée pourquoi ils ont utilisé "String" au lieu de "string", mais le reste est correct.
double amount = 31.245678;
amount = Math.Floor(amount * 100) / 100;
Vous pouvez utiliser cette
"String.Format (" {0: F2} "Valeur de chaîne);"
// give you only the two digit after Dot, excat two digit.
Vous pouvez également utiliser l'opérateur composite F indiquant alors le nombre de points décimales que vous souhaitez apparaître après la virgule.
string.Format("{0:F2}", 123.456789); //123.46
string.Format("{0:F3}", 123.456789); //123.457
string.Format("{0:F4}", 123.456789); //123.4568
Il arrondira donc être au courant.
Je la documentation générale est notre source. Il y a une tonne d'autres opérateurs de formatage là aussi que vous pouvez consulter.
Source: https://msdn.microsoft .com / fr-fr / bibliothèque / dwhawy9k (v = vs.110) .aspx
Essayez ceci
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);
}
En utilisant la propriété de String
double value = 123.456789;
String.Format("{0:0.00}", value);
Note:. Ceci peut être utilisé pour afficher uniquement
Utilisation System.Math
double value = 123.456789;
System.Math.Round(value, 2);
Essayez ceci:
double result = Math.Round(24.576938593,2);
MessageBox.Show(result.ToString());
Sortie: 24,57
Solution simple:
double totalCost = 123.45678;
totalCost = Convert.ToDouble(String.Format("{0:0.00}", totalCost));
//output: 123.45
yourValue.ToString("0.00") will work.
Utilisation interpolation chaîne decimalVar:0.00
double doublVal = 123.45678;
Il y a deux façons.
-
pour l'affichage dans la chaîne:
String.Format("{0:0.00}", doublVal );
-
pour geting nouveau Double
doublVal = Convert.ToDouble(String.Format("{0:0.00}", doublVal ));