0

Data sample: 20.50 and 20.00 Total: 41.00

How can i get the exact number as 40.50 ? I can sum the column and show in label, however i can not solve the rounding problem.

If DataGridView1.RowCount > 1 Then
        Dim tutar As Integer = 0
        For index As Integer = 0 To DataGridView1.RowCount - 1
            total += Convert.ToInt32(DataGridView1.Rows(index).Cells(3).Value)
        Next
        Label1.Text = total.ToString("N2")
    End If
burak
  • 21
  • 1
  • 3

1 Answers1

0

I assume that the variable total is an Integer, try changing your total variable to type Double.

somewhere in your code..

Dim total As Double = 0

and then change this:

If DataGridView1.RowCount > 1 Then
    Dim tutar As Integer = 0
    For index As Integer = 0 To DataGridView1.RowCount - 1
        total += Convert.ToDouble(DataGridView1.Rows(index).Cells(3).Value.tostring)
    Next
    Label1.Text = total.ToString()
End If

and if you want to do some rounding, follow this link

Community
  • 1
  • 1
Codemunkeee
  • 1,585
  • 5
  • 17
  • 29
  • My mistake. There is no such thing 'tutar'. It's 'total'. ` If DataGridView1.RowCount > 1 Then Dim total As Double = 0 For index As Integer = 0 To DataGridView1.RowCount - 1 total += Convert.ToDouble(DataGridView1.Rows(index).Cells(3).Value) Next Label1.Text = total.ToString() End If` I tried that and didn't work. – burak Apr 01 '14 at 03:35
  • it's okay.. just change `total` variable to `double`, or you may also use `decimal`.. depending on your needs. – Codemunkeee Apr 01 '14 at 03:38
  • Tried double or decimal. Both didn't work. Keep rounding the result. – burak Apr 01 '14 at 03:40
  • I remove "=0" Dim total As Double = 0 from that and it works. Thank you. – burak Apr 01 '14 at 15:25