I have the following value:
43239.110000000001
I used this command:
txtSomatorio.Text = String.Format( "{0:#.#,##}", somatorio);
I achieved this:
43239,11
How do I display it like this?
43.239,11
I have the following value:
43239.110000000001
I used this command:
txtSomatorio.Text = String.Format( "{0:#.#,##}", somatorio);
I achieved this:
43239,11
How do I display it like this?
43.239,11
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
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 .
Just use, number 2 is the number of decimal places.
txtSomatorio.Text = somatorio.ToString("n2");
Result :
43.239.11