I know the 'N' and 'C' formations of C #, but they do not answer me.
I get 1000
of the database and would like it to format for 1.000
.
What I put in
.ToString("???")
I know the 'N' and 'C' formations of C #, but they do not answer me.
I get 1000
of the database and would like it to format for 1.000
.
What I put in
.ToString("???")
Only use the format n
, followed by the number of decimal places.
You can see all the possibilities in Custom Number Format Strings , on MSDN .
Using .ToString()
same looks like this:
numero.ToString("n0");
Note that the format will be sensitive to the culture of the application. So it might be interesting to define a specific culture.
Note that the CultureInfo
class is in the System.Globalization
namespace, you will probably need to include it in the code ( using System.Globalization
).
numero.ToString("n0", new CultureInfo("pt-BR"));
If you are using C # 6, you can use it
$"{numero:n0}"
If you have a previous version
string.Format("{0:n0}", numero);
Example:
using System.Globalization;
public class Program
{
public static void Main()
{
int numero = 50000;
// Com string interpolation (C# 6)
Console.WriteLine($"{numero:n0}");
// Com string.Format
Console.WriteLine(string.Format("{0:n0}", numero));
// -- Definindo a cultura
Console.WriteLine(string.Format(new CultureInfo("pt-BR"), "{0:n0}", numero));
// Com ToString()
Console.WriteLine(numero.ToString("n0"));
// -- Definindo a cultura
Console.WriteLine(numero.ToString("n0", new CultureInfo("pt-BR")));
}
}
You can use this way:
1000.ToString("0,0",new System.Globalization.CultureInfo(("pt-BR")));
In this case you need to pass the instance of CultureInfo
from pt-BR to it format in the Brazilian standard, otherwise it formats in the American standard
You can use the following:
int retorno = Convert.ToInt32("1000");
string.Format("{0:0,0}", retorno);