Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decimal positions in result c#

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.

like image 962
Marek Avatar asked Jan 13 '23 18:01

Marek


1 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"
like image 124
V4Vendetta Avatar answered Jan 21 '23 16:01

V4Vendetta