How to format a number in C# with thousands separators (spaces or commas)?
Hello everyone! I'm a beginner in C# and I've encountered a problem with number formatting. I have a variable of type `int` that stores a large number, for example, 1234567. I need to output this number to the console or form as "1 234 567" or "1,234,567" — with thousands separators.
I know that .NET has a `String.Format()` method, but I can't figure out how to apply it correctly. I tried writing something like:
```csharp
int number = 1234567;
string formatted = String.Format("{0:N}", number);
Console.WriteLine(formatted);
```
This outputs "1,234,567.00" — with two zeros after the decimal point. But I need no fractional part, just the integer with thousands separators. How do I remove the decimal places? And is it possible to use a space instead of a comma as the separator? Please advise.
No answers yet.
Log in to leave an answer
Blogs