Avatar of mathieu_cupryk
mathieu_cupryk
Flag 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
.NET ProgrammingC#ASP.NET

Avatar of undefined
Last Comment
ajitha75

8/22/2022 - Mon
margajet24


            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
ajitha75

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
margajet24


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

Open in new window

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!
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
MoreHeroic

Oh, dang looks like my response took too long to write.  Sorry about that other experts, looks like you have it well in hand.
margajet24


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

Open in new window

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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
mathieu_cupryk

ASKER
no body is right so far.
margajet24

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


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

Open in new window

margajet24

can you post your current code?..
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
mathieu_cupryk

ASKER
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);                          
                             
                    }
                   
                }
ajitha75

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