Solved

How to convert double to nice looking string?

Posted on 2004-09-10
9
231 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:Smoerble
9 Comments
 
LVL 15

Expert Comment

by:Timbo87
ID: 12031815
Try this:

string output = Math.Round(26729.3782, 2).ToString("#,###.##");
Console.WriteLine(output);
0
 
LVL 5

Assisted Solution

by:tomasX2
tomasX2 earned 150 total points
ID: 12032413
or try
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("de-DE" );
double d = 123456789012345;
System.Diagnostics.Debug.WriteLine( d.ToString( "N", ci ) );
0
 

Author Comment

by:Smoerble
ID: 12033450
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.
0
 
LVL 15

Accepted Solution

by:
Timbo87 earned 200 total points
ID: 12034791
string output = Math.Round(26729.3782, 2).ToString("#,###.00");
Console.WriteLine(output);
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Assisted Solution

by:a_goat
a_goat earned 150 total points
ID: 12044805
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.
0
 
LVL 15

Expert Comment

by:Timbo87
ID: 12048281
It doesn't screw up no matter how large the input is. #,### is just a pattern.

Input:
2672954354354.3782

Output:
2,672,954,354,354.38
0
 
LVL 15

Expert Comment

by:Timbo87
ID: 12245106
I believe my last comment solves his question.
0
 

Author Comment

by:Smoerble
ID: 12257122
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now