Solved

Format string (double) in C#.

Posted on 2010-11-18
2
500 Views
Last Modified: 2013-12-17
Normally, I use this way to format a string in C#:
MainBoardImportBOMFG.Text = String.Format("{0:#,###.##}", Convert.ToDouble(myReader["FinishedGoodBOM"]));

If myReader["FinishedGoodBOM"] has value > 0, Ex: myReader["FinishedGoodBOM"] = 20000
MainBoardImportBOMFG.Text = 20,000
---> It's correct

But if  myReader["FinishedGoodBOM"] = 0 --> MainBoardImportBOMFG.Text = (nothing)
I want to display MainBoardImportBOMFG.Text = 0 when myReader["FinishedGoodBOM"] = 0
How can I do?

0
Comment
Question by:JameMeck
2 Comments
 
LVL 10

Accepted Solution

by:
Mathiyazhagan earned 500 total points
ID: 34162716
use zero placeholder in your format string like :

            double dbl =10000;
            string ds =String.Format("{0:#,##0.00}", dbl);

if dbl = 1       then ds = 1.00
if dbl = 10     then ds = 10.00
if dbl = 100   then ds = 100.00
if dbl = 1000  then ds = 1,000.00
if dbl = 10000 then ds = 10,000.00
if dbl = 100000  then ds = 100,000.00
and if dbl = 0 then ds = 0.00

here we specifying # to custom holder and single 0 before . to display atleast one degit before .00
hope this helps
0
 

Author Closing Comment

by:JameMeck
ID: 34170260
Thanks! It's a perfect solution.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

822 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