Hi
I have a numeric column on a datagrid that I am putting into a textbox, The problem is that the value has 4 decimal places and I want to show no decimals. I am not sure though how to convert it to a decimal. pPrem_Optimized in the grid has 4 decimals I want it to show in the textbox with none. Here is the line does anyone know how to limit it to no decimals?
prem_OptimizedTextBox.Text = Grid_Property.Rows[e.RowIndex].Cells["pPrem_Optimized"].Value.ToString();
Thanks

1 answers
It depends on what type of value is actually held in the cell. For example, if it's a double and you only want to truncate the value (not round it to the nearer integer), you could do:
double temp = (double)Grid_Property.Rows[e.RowIndex].Cells["pPrem_Optimized"].Value;
prem_OptimizedTextBox.Text = temp.ToString("F0");
Alternatively, whatever type of vaue it is, you could use string parsing to remove the part of the number from the decimal point onwards:
string temp = Grid_Property.Rows[e.RowIndex].Cells["pPrem_Optimized"].Value.ToString();
int index = temp.IndexOf('.');
if (index > -1)
{
temp = temp.Substring(0, index);
}
prem_OptimizedTextBox.Text = temp;
answered 2 years ago by:
17279
Hi Thanks,
Actually I do want to round it to the nearest whole number.
answered 2 years ago by:
0
The first thing that comes to mind is maybe this:
Math.Round(myDecimal, 0)
should give you 0 places places past the decimal. However, if Vulpes advises you differently, go with whatever he says.
answered 2 years ago by:
0
This should work too:
Double dd = 1.9234;
string s = dd.ToString("#");
MessageBox.Show(s);
answered 2 years ago by:
0
This post was imported from csharpfriends, if you have a similiar question please ask it again.
All previous members have been migrated, hope you enjoy the new platform!