Wie formatiere ich eine Zahl mit Kommas?
Frage
int a = 10000000;
a.ToString();
Wie kann ich die Ausgabe machen?
10000000
Lösung
Versuchen N0
ohne Dezimalteil:
string formatted = a.ToString("N0"); // 10,000,000
Andere Tipps
Sie können auch tun String.Format:
int x = 100000;
string y = string.Empty;
y = string.Format("{0:#,##0.##}", x);
//Will output: 100,000
Wenn Sie dezimal, der gleiche Code gibt 2 Dezimalstellen:
double x = 100000.2333;
string y = string.Empty;
y = string.Format("{0:#,##0.##}", x);
//Will output: 100,000.23
Um Komma statt Dezimal Verwendung macht dies:
double x = 100000.2333;
string y = string.Empty;
y = string.Format(System.Globalization.CultureInfo.GetCultureInfo("de-DE"), "{0:#,##0.##}", x);
a.ToString("N0")
Siehe auch: Standard Numerisch Formatierung Strings von MSDN
Eine einfachere String.Format Option:
int a = 10000000;
String.Format("{0:n0}", a); //10,000,000
a.tostring ( "00000000")
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow