3
I know the 'N' and 'C' formatting of C#, but they do not suit me.
I get 1000
database and would like you to format to 1.000
.
What I put in the
.ToString("???")
3
I know the 'N' and 'C' formatting of C#, but they do not suit me.
I get 1000
database and would like you to format to 1.000
.
What I put in the
.ToString("???")
6
Only use the formatting n
, followed by the number of decimals.
You can see all the possibilities in custom numeric format string in MSDN.
Using the .ToString()
even so:
numero.ToString("n0");
Note that the format will be sensitive to the application culture. So it may be interesting to define a specific culture.
Note that the class CultureInfo
is in namespace System.Globalization
, 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 like this
$"{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")));
}
}
1
You can use it like this:
1000.ToString("0,0",new System.Globalization.CultureInfo(("pt-BR")));
In this case you need to pass the instance of CultureInfo
of en-BR for it to format in the Brazilian standard, otherwise it formats in the American standard
0
You can use the following:
int retorno = Convert.ToInt32("1000");
string.Format("{0:0,0}", retorno);
Browser other questions tagged c# string formatting
You are not signed in. Login or sign up in order to post.