Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting strings into groups of 3 digits by periods

Tags:

c#

.net

asp.net

I know I can format strings using the String.Format() method. Is it possible to format like this?

Example:

string: 1568
formatted: 1.568

string: 168794521
formatted: 168.794.521

string: 987
formatted: 987

Sorry that I can't make myself more clear.

like image 280
ShadowG Avatar asked Dec 28 '25 15:12

ShadowG


1 Answers

You can format a number that way, but not a string. For example, if you have an integer value, you can use:

int value = 168794521;
string formatted = value.ToString("N0");

With the proper culture, this will format as shown.

If you are using a string, you would need to convert it. You could also explicitly provide a culture to guarantee "." as a thousands separator:

int value = Int32.Parse("168794521");
string formatted = value.ToString("N0", new CultureInfo("de-DE"));
like image 89
Reed Copsey Avatar answered Dec 31 '25 06:12

Reed Copsey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!