How to convert double to nice looking string?

Hi all,
can you please help me to find a smart way to convert doubles into nice looking strings?

Example: 26729.3782 should become: 26.729,37

(german format uses "," instead of ".", the "." is used every 3rd char from end of number (not counting chars behind the ",")

So here are some examples, I hope, someone has already a solution for this:
26729.3782  ==>  26.729,37
0                 ==>  0,00
234.5           ==> 234,50
1234567890  ==> 1.234.567.890,00

Any thoughts? Thank you.
Who is Participating?
Timbo87Connect With a Mentor Commented:
string output = Math.Round(26729.3782, 2).ToString("#,###.00");
Try this:

string output = Math.Round(26729.3782, 2).ToString("#,###.##");
tomasX2Connect With a Mentor Commented:
or try
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("de-DE" );
double d = 123456789012345;
System.Diagnostics.Debug.WriteLine( d.ToString( "N", ci ) );
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

SmoerbleAuthor Commented:
Thank you guys.
if I go with the solution from Timbo87, is there also a way to easily "force" to have 2 digits behind the comma (so if it's an integer, tha the string becomes 1.234,00)?

Thank you so far.
a_goatConnect With a Mentor Commented:
The RIGHT way is tomasX2's.  The other way will screw you up once you get to the millions.  In fact, the REALLY right way is:

System.Diagnostics.Debug.WriteLine( d.ToString( "N", System.Globalization.CultureInfo.InstalledUICulture ) );

That way it'll do it the right way for you based on how you set your computer up, no matter where you are.
It doesn't screw up no matter how large the input is. #,### is just a pattern.


I believe my last comment solves his question.
SmoerbleAuthor Commented:
Yes, somehow ;)
I need it in german format which uses "." instead of "," and vice versa. So the solution from aGoat seems to be more the "real" way... but for simplicity reasons I go with your solution for now.

To giev each of you a fair amount of points, I increase to 500 and split the points, thank you.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.