Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to convert double to nice looking string?

Posted on 2004-09-10
9
233 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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
 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

860 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