Link to home
Start Free TrialLog in
Avatar of mathieu_cupryk
mathieu_cuprykFlag for Canada

asked on

Need to format to two decimal places

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
Avatar of margajet24
margajet24
Flag of Singapore image


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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ajitha75
ajitha75
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

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

Open in new window

Avatar of MoreHeroic
MoreHeroic

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.

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

Open in new window

Avatar of mathieu_cupryk

ASKER

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
no body is right so far.
ignore my last post.. i accidentally hit the button.. it should have been  :


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

Open in new window

can you post your current code?..
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);                          
                             
                    }
                   
                }
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