Solved

How to convert double to nice looking string?

Posted on 2004-09-10
9
230 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
string output = Math.Round(26729.3782, 2).ToString("#,###.00");
Console.WriteLine(output);
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 3

Assisted Solution

by:a_goat
a_goat earned 150 total points
Comment Utility
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
Comment Utility
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
Comment Utility
I believe my last comment solves his question.
0
 

Author Comment

by:Smoerble
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…

744 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

16 Experts available now in Live!

Get 1:1 Help Now