Reputation: 7087
Hopefully a simple answer although a web search hasn't yielded anything as of yet.
To format a number with thousands you simply use:
var output = String.Format("{0:n0}", 1000000);
// Output 1,000,000 or 1.000.000 depending on Culture
To format a number to always display a sign at the front you can use:
var output = String.Format("{0:+0;-0;+0}", 1);
// Output +1;
var output = String.Format("{0:+0;-0;+0}", -1);
// Output -1;
So how do I format a number with a sign and with a thousand separator?
Logically it should be this, but it doesn't work:
var output = String.Format("{0:+n0;-n0;+0}", -1000000);
// Expected output -1,000,000
// Actual output -n10000000
Is there a format string that allows this and respects the current locale where the thousands separator could be a .
or a ,
?
Upvotes: 0
Views: 238
Reputation: 148
You could give this a try:
int num = -5000000;
Console.WriteLine(num.ToString("N0"));
Edit: My apologies! I was only focused on the negative symbol. Give this a shot:
string format = "+##,#;-##,#";
int num = -5000000;
int num2 = 5000000;
Console.WriteLine(num.ToString(format));
Console.WriteLine(num2.ToString(format));
Upvotes: 0