mathieu_cupryk
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].Item Array[5].T oString()) ;
sw.Write(",");
There is an attached file with values in the array.
7-19-2008-9-52-28-AM.gif
dt.Rows[row].ItemArray[5].
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].Item
sw.Write(",");
There is an attached file with values in the array.
7-19-2008-9-52-28-AM.gif
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
string abc = "123.3213";
Decimal dec = Decimal.Parse(abc);
Console.WriteLine("{0}",string.Format("{0:F}", dec));
I'm away from my IDE so this may need a little tweaking but try something like this:
sw.Write(Convert.ToDecimal (dt.Rows[r ow].ItemAr ray[5]).To String("F" , CultureInfo.InvariantCultu re));
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!
sw.Write(Convert.ToDecimal
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();
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
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
ASKER
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"));
can you post your current code?..
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].Item Array[2].T oString(). TrimEnd()) ;
sw.Write(",");
// Basis 2
if (dt.Rows[row].ItemArray[0] .ToString( ) == "01")
sw.Write(dt.Rows[row].Item Array[23]. ToString() );
if (dt.Rows[row].ItemArray[0] .ToString( ) == "02")
sw.Write(dt.Rows[row].Item Array[24]. ToString() );
if (dt.Rows[row].ItemArray[0] .ToString( ) == "04")
sw.Write(dt.Rows[row].Item Array[25]. ToString() );
if (dt.Rows[row].ItemArray[0] .ToString( ) == "64")
sw.Write(dt.Rows[row].Item Array[26]. ToString() );
sw.Write(",");
// Crop Year 3
sw.Write(_priceListHeaderI D.Substrin g(0, 4));
sw.Write(",");
// Suffix Code 4
sw.Write(dt.Rows[row].Item Array[1].T oString()) ;
sw.Write(",");
// Pool Code 5
sw.Write(dt.Rows[row].Item Array[0].T oString(). PadLeft(2, '0'));
sw.Write(",");
// Protein 1 decimal place.
sw.Write(dt.Rows[row].Item Array[3].G etType().T oString()) ;
sw.Write(",");
// Straight Straight Grade Code 7
sw.Write(dt.Rows[row].Item Array[4].T oString()) ;
sw.Write(",");
// Straight Amount 8 2 decimal place
sw.Write(dt.Rows[row].Item Array[5].T oString()) ;
sw.Write(",");
// Tough Grade Code 9
sw.Write(dt.Rows[row].Item Array[6].T oString()) ;
sw.Write(",");
// Tough Code Amount added to straight 10 2 decimal place
sw.Write(clsLoadDiscounts. LoadSaleDi scounts("T OUGH", dt.Rows[row]));
sw.Write(",");
// Damp Grade Code 11
sw.Write(dt.Rows[row].Item Array[7].T oString()) ;
sw.Write(",");
// Damp Code Amount added to straight 12 2 decimal place
sw.Write(clsLoadDiscounts. LoadSaleDi scounts("D AMP", dt.Rows[row]));
sw.Write(",");
// Stone Grade Code 13 2 decimal place
sw.Write(dt.Rows[row].Item Array[8].T oString()) ;
sw.Write(",");
// Stone Code Amount added to straight 14 2 decimal place
sw.Write(clsLoadDiscounts. LoadSaleDi scounts("S TONE", dt.Rows[row]));
sw.Write(",");
// Tough Stone Code 15
sw.Write(dt.Rows[row].Item Array[9].T oString()) ;
sw.Write(",");
// Stone Code Amount added to straight 16 2 decimal place
sw.Write(clsLoadDiscounts. LoadSaleDi scounts("T OUGH STONE", dt.Rows[row]));
sw.Write(",");
// Damp Stone Code 17
sw.Write(dt.Rows[row].Item Array[10]. ToString() );
sw.Write(",");
// Damp Stone Amount added to straight 18 2 decimal place
sw.Write(clsLoadDiscounts. LoadSaleDi scounts("D AMP STONE", dt.Rows[row]));
sw.Write(sw.NewLine);
}
}
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].Item
sw.Write(",");
// Basis 2
if (dt.Rows[row].ItemArray[0]
sw.Write(dt.Rows[row].Item
if (dt.Rows[row].ItemArray[0]
sw.Write(dt.Rows[row].Item
if (dt.Rows[row].ItemArray[0]
sw.Write(dt.Rows[row].Item
if (dt.Rows[row].ItemArray[0]
sw.Write(dt.Rows[row].Item
sw.Write(",");
// Crop Year 3
sw.Write(_priceListHeaderI
sw.Write(",");
// Suffix Code 4
sw.Write(dt.Rows[row].Item
sw.Write(",");
// Pool Code 5
sw.Write(dt.Rows[row].Item
sw.Write(",");
// Protein 1 decimal place.
sw.Write(dt.Rows[row].Item
sw.Write(",");
// Straight Straight Grade Code 7
sw.Write(dt.Rows[row].Item
sw.Write(",");
// Straight Amount 8 2 decimal place
sw.Write(dt.Rows[row].Item
sw.Write(",");
// Tough Grade Code 9
sw.Write(dt.Rows[row].Item
sw.Write(",");
// Tough Code Amount added to straight 10 2 decimal place
sw.Write(clsLoadDiscounts.
sw.Write(",");
// Damp Grade Code 11
sw.Write(dt.Rows[row].Item
sw.Write(",");
// Damp Code Amount added to straight 12 2 decimal place
sw.Write(clsLoadDiscounts.
sw.Write(",");
// Stone Grade Code 13 2 decimal place
sw.Write(dt.Rows[row].Item
sw.Write(",");
// Stone Code Amount added to straight 14 2 decimal place
sw.Write(clsLoadDiscounts.
sw.Write(",");
// Tough Stone Code 15
sw.Write(dt.Rows[row].Item
sw.Write(",");
// Stone Code Amount added to straight 16 2 decimal place
sw.Write(clsLoadDiscounts.
sw.Write(",");
// Damp Stone Code 17
sw.Write(dt.Rows[row].Item
sw.Write(",");
// Damp Stone Amount added to straight 18 2 decimal place
sw.Write(clsLoadDiscounts.
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?
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