Solved

# How to convert double to nice looking string?

Posted on 2004-09-10
230 Views
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
Question by:Smoerble

LVL 15

Expert Comment

Try this:

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

LVL 5

Assisted Solution

tomasX2 earned 150 total points
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

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

Timbo87 earned 200 total points
string output = Math.Round(26729.3782, 2).ToString("#,###.00");
Console.WriteLine(output);
0

LVL 3

Assisted Solution

a_goat earned 150 total points
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

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

I believe my last comment solves his question.
0

Author Comment

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

### Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…