How to add thousands separators (commas) to a number when formatting a string in C#?
Hi everyone! I'm a beginner C# developer and I've encountered a task: I need to display a number with thousands separators — that is, the number 1234567 should be displayed as "1,234,567". I'm using `String.Format()` or simply inserting a variable into a string via `$""`, but I can't achieve the desired result yet.
Here's an example of my code:
```csharp
int number = 1234567;
string result = String.Format("Sum: {0}", number);
Console.WriteLine(result);
```
The output is just "Sum: 1234567", but I need "Sum: 1,234,567". I tried adding different format specifiers to the format string, but so far without success. Please advise how to do this correctly? Preferably with code examples so I can test it right away. Thanks in advance!
<<>>
Using the "N" or "n" format specifier
No answers yet.
Log in to leave an answer