Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2007

Tip: Looking for answers? Try searching our database.

string formatting question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tenacious - 06 Dec 2007 17:45 GMT
Can anyone tell me why strSpd2 in the example below ends up being a
blank string when formatting a zero value? It would seem to me that
strSpd2 should end up being "0.0"

double val1 = 14.38009
double val2 = 0.0;
string strSpd1 = "";
string strSpd2 = "";

// This formats correctly. strSpd1 = "14.4"
strSpd1 = String.Format("{0:##.#}", val1);

// This does not format. strSpd2 = ""  Why is this blank and not "0.0"
strSpd2 = String.Format("{0:#.#}", val2);
Tom Porterfield - 06 Dec 2007 18:01 GMT
> Can anyone tell me why strSpd2 in the example below ends up being a
> blank string when formatting a zero value? It would seem to me that
[quoted text clipped - 10 lines]
> // This does not format. strSpd2 = ""  Why is this blank and not "0.0"
> strSpd2 = String.Format("{0:#.#}", val2);

If you look at the documentation on formatting and the # format
character, you'll find the following information:

"Note that this specifier never displays the '0' character if it is not
a significant digit, even if '0' is the only digit in the string."

Use strSpd2 = String.Format("{0:0.0}", val2); instead.
Signature

Tom Porterfield

Ole - 06 Dec 2007 20:02 GMT
Or use:

string strSpd2 = val2.ToString("0.0#");

BR,
Ole

> Can anyone tell me why strSpd2 in the example below ends up being a
> blank string when formatting a zero value? It would seem to me that
[quoted text clipped - 10 lines]
> // This does not format. strSpd2 = ""  Why is this blank and not "0.0"
> strSpd2 = String.Format("{0:#.#}", val2);

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.