Solved

Format string (double) in C#.

Posted on 2010-11-18
2
502 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
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…

828 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