We help IT Professionals succeed at work.

Need to format to two decimal places

922 Views
Last Modified: 2013-12-17
I have the following:

            dt.Rows[row].ItemArray[5].ToString()      "138.50"      string
I always have to make sure it is formated to two decimal places.
See array for item 5.
 // Straight Amount 8 2 decimal place
                       sw.Write(dt.Rows[row].ItemArray[5].ToString());
                       sw.Write(",");
There is an attached file with values in the array.

7-19-2008-9-52-28-AM.gif
Comment
Watch Question

margajet24IT Business Analyst

Commented:

            string abc = "123.3213";
 
            Decimal dec = Decimal.Parse(abc);
 
            Console.WriteLine("{0}",string.Format("{0:N2}", dec));
 
            Console.ReadLine();

Open in new window

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
margajet24IT Business Analyst

Commented:

            string abc = "123.3213";
 
            Decimal dec = Decimal.Parse(abc);
 
            Console.WriteLine("{0}",string.Format("{0:F}", dec));

Open in new window

I'm away from my IDE so this may need a little tweaking but try something like this:

sw.Write(Convert.ToDecimal(dt.Rows[row].ItemArray[5]).ToString("F", CultureInfo.InvariantCulture));
sw.Write(",");

That way, you're converting the object to a decimal and then formatting the string output from the decimal to F.  For helpful links check out the following:

http://msdn.microsoft.com/en-us/library/0b22a4x2.aspx - Info about Decimal.toString() method
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx - Infor about standard number format strings

Hope that helps!
Oh, dang looks like my response took too long to write.  Sorry about that other experts, looks like you have it well in hand.
margajet24IT Business Analyst

Commented:

            string abc = "123.3213";
 
            Decimal dec = Decimal.Parse(abc);
 
            Console.WriteLine("{0}",dec.ToString(".000"));
 
            Console.ReadLine();

Open in new window

Author

Commented:
Marge it does not work.
I need two decimal places in my csv document.

138.5
139.1
139.7
140.3
140.9
141.5
142.1
142.7
143.3
143.9
144.5
145.1
145.7
146.3
146.9
147.5
148.1
148.7
149.3
149.9
150.5
151.1
151.7
152.3
152.9
153.7
154.5
155.3
156.1
156.9
157.7
158.5
159.3
160.1
160.9
161.95
163
164.05
165.1

Author

Commented:
no body is right so far.
margajet24IT Business Analyst

Commented:
ignore my last post.. i accidentally hit the button.. it should have been  :


Console.WriteLine("{0}",dec.ToString(".00"));

Open in new window

margajet24IT Business Analyst

Commented:
can you post your current code?..

Author

Commented:
Everywhere where it says two decimal place.

foreach (System.Data.DataTable dt in DSReadyToLoad.Tables)
                {

                    // English side.
                    for (int row = 1; row < dt.Rows.Count; row++)
                    {
                       // Grade Class 1
                       sw.Write(dt.Rows[row].ItemArray[2].ToString().TrimEnd());
                       sw.Write(",");
                       
                       // Basis 2
                       if (dt.Rows[row].ItemArray[0].ToString() == "01")
                           sw.Write(dt.Rows[row].ItemArray[23].ToString());
                       if (dt.Rows[row].ItemArray[0].ToString() == "02")
                           sw.Write(dt.Rows[row].ItemArray[24].ToString());
                       if (dt.Rows[row].ItemArray[0].ToString() == "04")
                           sw.Write(dt.Rows[row].ItemArray[25].ToString());
                       if (dt.Rows[row].ItemArray[0].ToString() == "64")
                           sw.Write(dt.Rows[row].ItemArray[26].ToString());
                       sw.Write(",");
                     
                      // Crop Year 3
                       sw.Write(_priceListHeaderID.Substring(0, 4));
                       sw.Write(",");
                     
                       // Suffix Code 4
                       sw.Write(dt.Rows[row].ItemArray[1].ToString());
                       sw.Write(",");

                       // Pool Code 5
                       sw.Write(dt.Rows[row].ItemArray[0].ToString().PadLeft(2, '0'));
                       sw.Write(",");

                       // Protein 1 decimal place.
                       sw.Write(dt.Rows[row].ItemArray[3].GetType().ToString());
                       sw.Write(",");

                       // Straight Straight Grade Code 7
                       sw.Write(dt.Rows[row].ItemArray[4].ToString());
                       sw.Write(",");

                       // Straight Amount 8 2 decimal place
                       sw.Write(dt.Rows[row].ItemArray[5].ToString());
                       sw.Write(",");

                       // Tough Grade Code 9
                       sw.Write(dt.Rows[row].ItemArray[6].ToString());
                       sw.Write(",");

                       // Tough Code Amount added to straight 10 2 decimal place    
                       sw.Write(clsLoadDiscounts.LoadSaleDiscounts("TOUGH", dt.Rows[row]));
                       sw.Write(",");


                       // Damp Grade Code 11
                       
                       sw.Write(dt.Rows[row].ItemArray[7].ToString());
                       sw.Write(",");

                       // Damp Code Amount added to straight 12 2 decimal place
                       sw.Write(clsLoadDiscounts.LoadSaleDiscounts("DAMP", dt.Rows[row]));
                       sw.Write(",");


                       // Stone Grade Code 13 2 decimal place
                       sw.Write(dt.Rows[row].ItemArray[8].ToString());
                       sw.Write(",");

                       // Stone Code Amount added to straight 14 2 decimal place  
                       sw.Write(clsLoadDiscounts.LoadSaleDiscounts("STONE", dt.Rows[row]));
                       sw.Write(",");

                       
                       // Tough Stone Code 15
                       sw.Write(dt.Rows[row].ItemArray[9].ToString());
                       sw.Write(",");

                       // Stone Code Amount added to straight 16  2 decimal place
                       sw.Write(clsLoadDiscounts.LoadSaleDiscounts("TOUGH STONE", dt.Rows[row]));
                       sw.Write(",");

                       
                       // Damp Stone Code 17
                       sw.Write(dt.Rows[row].ItemArray[10].ToString());
                       sw.Write(",");


                       // Damp Stone Amount added to straight 18  2 decimal place
                       sw.Write(clsLoadDiscounts.LoadSaleDiscounts("DAMP STONE", dt.Rows[row]));
                       sw.Write(sw.NewLine);                          
                             
                    }
                   
                }

Commented:
The attached code is giving the below result

138.50
139.10
139.70
140.30
140.90
141.50
142.10
142.70
143.30
143.90
144.50
145.10
145.70
146.30
146.90
147.50
148.10
148.70
149.30
149.90
150.50
151.10
151.70
152.30
152.90
153.70
154.50
155.30
156.10
156.90
157.70
158.50
159.30
160.10
160.90
161.95
163.00
164.05
165.10

Are you saying this is not working in your code?

      private void button1_Click(object sender, EventArgs e)
        {
            string[] s = {"138.5",
                            "139.1",
                            "139.7",
                            "140.3",
                            "140.9",
                            "141.5",
                            "142.1",
                            "142.7",
                            "143.3",
                            "143.9",
                            "144.5",
                            "145.1",
                            "145.7",
                            "146.3",
                            "146.9",
                            "147.5",
                            "148.1",
                            "148.7",
                            "149.3",
                            "149.9",
                            "150.5",
                            "151.1",
                            "151.7",
                            "152.3",
                            "152.9",
                            "153.7",
                            "154.5",
                            "155.3",
                            "156.1",
                            "156.9",
                            "157.7",
                            "158.5",
                            "159.3",
                            "160.1",
                            "160.9",
                            "161.95",
                            "163",
                            "164.05",
                            "165.1"};
            foreach (String s1 in s)
            {
                textBox1.Text += Convert.ToDouble(s1).ToString(".00") + Environment.NewLine;
            }
        }

Open in new window

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.