I have got this code which calculates the summary of 2 multiplied columns of all rows in datagridview.
private void vypocti_odvody(int price, int vat, int tot_rows2)
{
try
{
double outVal = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
outVal = outVal + (Convert.ToDouble(row.Cells[price].Value)/100) * Convert.ToDouble(row.Cells[vat].Value) + (Convert.ToDouble(row.Cells[price].Value));
}
// s_ub_celk.Text = (((a / 100) * b) + a).ToString();
Convert.ToDecimal ( result.Text = outVal.ToString()) ;
}
catch (Exception ex)
{
MessageBox.Show("Chybové hlášení K3 " + ex.Message.ToString());
}
}
How can I improve decimal positions? Now the result is like: 123,5484250 I would like to have only 2 decimal positions with result for example 123,55.
Sorry for my poor english I hope 'decimal position' is express for word that I really mean. In case I made an example for better understanding.
Thank you everyone for your time, comments and answers.
Since you are using DataGridView
you can set the format of the cell like
DataGridView1.Columns[required column index or name].DefaultCellStyle.Format = "N2"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With