Como faço para formatar um número com vírgulas?
Pergunta
int a = 10000000;
a.ToString();
Como posso fazer a saída?
10000000
Solução
N0
tentar por nenhuma parte decimal:
string formatted = a.ToString("N0"); // 10,000,000
Outras dicas
Você também pode fazer String.Format:
int x = 100000;
string y = string.Empty;
y = string.Format("{0:#,##0.##}", x);
//Will output: 100,000
Se você tem decimal, o mesmo código vontade de saída 2 casas decimais:
double x = 100000.2333;
string y = string.Empty;
y = string.Format("{0:#,##0.##}", x);
//Will output: 100,000.23
Para fazer vírgula em vez de uso decimal isto:
double x = 100000.2333;
string y = string.Empty;
y = string.Format(System.Globalization.CultureInfo.GetCultureInfo("de-DE"), "{0:#,##0.##}", x);
a.ToString("N0")
Veja também: padrão numérico formatação Cordas da MSDN
A opção String.Format mais simples:
int a = 10000000;
String.Format("{0:n0}", a); //10,000,000
a.tostring ( "00000000")
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow