Format Field C #

3

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("???")
    
asked by anonymous 07.06.2017 / 17:59

3 answers

5

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:

See working in .NET Fiddle.

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")));
    }
}
    
07.06.2017 / 18:03
1

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

    
07.06.2017 / 18:07
0

You can use the following:

   int retorno = Convert.ToInt32("1000");
   string.Format("{0:0,0}", retorno);
    
07.06.2017 / 23:24