decimal Conversion Problems
Hello,
I can't seem to get my code to work. I can't seem to display my values correctly after a conversion. It seems to be a rounding error.
But nothing I have tried works. Any ideas?
private void Calculate()
{
if(ActiveSelect == false)
{
dsPicked = null;
decimal dRatio = 0;
dsPicked = dsCherryPick.Clone();
//System.Text.StringBuilder sb = new StringBuilder(1024, 1024 * 5);
//sb.Append("CAD IN (");
if(gridProperties.Selected.Rows.Count > 0)
{
foreach(Infragistics.Win.UltraWinGrid.UltraGridRow rw in gridProperties.Selected.Rows)
{
dsPicked.Tables[0].Rows.Add(Main.dsCherryPick.Tables[0].Rows.Find(rw.Cells["CAD"].Value).ItemArray);
//sb.Append("'" + rw.Cells["CAD"].Text + "', ");
}
//sb.Append (" '1' )");
//System.Data.DataRow[] dr2 = Main.dsCherryPick.Tables[0].Select(sb.ToString(),"Ratio");
dsPicked.Tables[0].AcceptChanges();
dsPicked.Tables[0].DefaultView.Sort = "Ratio";
dsPicked = SortDS(dsPicked);
if(Profile != "Vacant Land")
{
//int retMod = mod(dsPicked.Tables[0].Rows.Count,2);
int iRowCnt = dsPicked.Tables[0].Rows.Count;
int retMod = (dsPicked.Tables[0].Rows.Count % 2);
if (retMod != 0)
{
dRatio = Convert.ToDecimal(dsPicked.Tables[0].Rows[iRowCnt/2]["Ratio"]);
//dRatio = Math.Round(dRatio,3);
txtRatio.Text = String.Format("{0:P}", dRatio);
}
else
{
//retMod = retMod - 1;
decimal tempRatio = System.Convert.ToDecimal(dsPicked.Tables[0].Rows[iRowCnt/2]["Ratio"]);
dRatio = System.Convert.ToDecimal(dsPicked.Tables[0].Rows[(iRowCnt/2)-1]["Ratio"]);
//dRatio = Math.Round(dRatio,3);
dRatio = (tempRatio + dRatio)/2;
txtRatio.Text = String.Format("{0:P}",dRatio);
}
decimal d1 = Convert.ToDecimal(gridSubject.Rows[0].Cells["TotalPerSqft"].Value);
//d1 = Math.Round(d1,3);
txtMedPerSqft.Text = String.Format("{0:C}", d1 * dRatio);
int d3 = System.Convert.ToInt32 (gridSubject.Rows[0].Cells["sqft"].Value);
decimal d4 = d3 * (d1 * dRatio);
txtSubjectProposed.Text = String.Format("{0:C}", d4);
//txtMedPerSqft.Text = String.Format("{0:C}",System.Convert.ToDouble(txtMedPerSqft.Text));
dsTotals.Tables[0].Clear();
System.Data.DataRow drtotal = dsTotals.Tables[0].NewRow();
drtotal["Median"] = dRatio; //Shared.fixdecimal(txtRatio.Text);
drtotal["MedAdjustSF"] = d4; //Shared.fixdecimal(txtMedPerSqft.Text);
dsTotals.Tables[0].Rows.Add(drtotal);
}
}
}
}