¿Cómo puedo dar formato a un número con comas?
Pregunta
int a = 10000000;
a.ToString();
¿Cómo hago la salida?
10000000
Solución
Trate N0
por ninguna parte decimal:
string formatted = a.ToString("N0"); // 10,000,000
Otros consejos
También se puede hacer String.Format:
int x = 100000;
string y = string.Empty;
y = string.Format("{0:#,##0.##}", x);
//Will output: 100,000
Si usted tiene decimal, el mismo código dará salida a 2 decimales:
double x = 100000.2333;
string y = string.Empty;
y = string.Format("{0:#,##0.##}", x);
//Will output: 100,000.23
Para hacer uso de la coma decimal en vez esto:
double x = 100000.2333;
string y = string.Empty;
y = string.Format(System.Globalization.CultureInfo.GetCultureInfo("de-DE"), "{0:#,##0.##}", x);
a.ToString("N0")
Vea también: estándar numérico de formato de cadenas desde MSDN
Una opción más simple String.Format:
int a = 10000000;
String.Format("{0:n0}", a); //10,000,000
a.tostring ( "00000000")
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow