Format double with thousands and decimal


I have the following value:


I used this command:

txtSomatorio.Text = String.Format( "{0:#.#,##}", somatorio);

I achieved this:


How do I display it like this?

asked by anonymous 23.12.2014 / 18:45

3 answers


Try to use this way:

string.Format("{0:0,0.00}", somatorio); // saída = 43.239,11

Or so:

string.Format("{0:N}", 43239,11)); // saída = 43.239,11
23.12.2014 / 18:48

The two responses posted are correct, but there is an addendum to the culture used. The answers show how normal it is to be used if you are sure that a computer is set up with a culture that gives the result you expect or even if what you expect actually is not the format informed but the monetary format that the user is waiting, that is, the format that is on his computer (which is a good idea)

If you want to make sure that the format is what you said on any computer, you need to tell the culture to be used in the code :

string.Format(CultureInfo.GetCultureInfo("pt-BR"), "{0:N}", 43239.11));

You need to use namespace System.Globalization .

You can even create a new culture the way you like it best: using System.Globalization;

public class Program {
    public static void Main() {
        var minhaCultura = new CultureInfo("pt-BR"); 
        minhaCultura.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
        minhaCultura.DateTimeFormat.ShortTimePattern = "HH:mm";
        minhaCultura.NumberFormat.NumberDecimalDigits = 2;
        minhaCultura.NumberFormat.NumberGroupSeparator = "_";
        minhaCultura.NumberFormat.NumberDecimalSeparator = ",";
        System.Console.WriteLine(string.Format(minhaCultura, "{0:N}", 43239.11));

See running on .NET Fiddle . And no Coding Ground . Also I placed GitHub for future reference .

Documentation .

23.12.2014 / 19:02

Just use, number 2 is the number of decimal places.

txtSomatorio.Text = somatorio.ToString("n2");

Result :

23.12.2014 / 18:48