blah blah blah is here! blah blah » Close

up0down
link

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

last answered 2 years ago

1 answers

up0down
link

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;

up0down
link

Hi Thanks,
Actually I do want to round it to the nearest whole number.

up0down
link

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.

up0down
link

This should work too:
Double dd = 1.9234;
string s = dd.ToString("#");
MessageBox.Show(s);

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!

Feedback