2017-09-26 7 views
0

Versuch prozentuale Menge zu berechnen, aber es ist immer eine 0.Fehler bei der Berechnung Prozentsatz

foreach (DataGridViewRow Reihe in dataGridView1.Rows) return { Doppel igst_amt;

  double amt = (Convert.ToDouble(row.Cells[dataGridView1.Columns[4].Index].Value)) * (Convert.ToDouble(row.Cells[dataGridView1.Columns[5].Index].Value)); 
      row.Cells[dataGridView1.Columns[6].Index].Value = Convert.ToString(amt); 
      igst_amt = (igst/100)*(amt); 
      MessageBox.Show(Convert.ToString(igst_amt)); 
      if (state == "o") 
      { 
       row.Cells[dataGridView1.Columns[9].Index].Value =Convert.ToString((cgst/100) *(amt)); 
       row.Cells[dataGridView1.Columns[11].Index].Value =Convert.ToString((sgst/100) * (amt)); 
      } 
      else if (state == "i") 
      { 
       row.Cells[dataGridView1.Columns[7].Index].Value = igst_amt; 
      } 
      double g_total = igst_amt + amt; 
      row.Cells[dataGridView1.Columns[13].Index].Value = Convert.ToString(g_total); 
      double t_g_total=0; 
      t_g_total+= g_total; 
     } 
+0

Was ist die Art von 'igst'? –

+0

Datentyp ist doppelt double igst = 0; Doppel-Cgst = 0; doppelt sgst = 0; –

+0

Wird "igst" nicht null? –

Antwort

0

Lassen Sie sich dies bricht auf den grundlegenden Code nach unten erforderlich, um das Problem zu reproduzieren:

int igst = 10; 
double amt = 42; 
double igst_amt = (igst/100) * (amt); 

Console.WriteLine(igst_amt); 

Damit ich ein Ergebnis von 0 bekommen.

Allerdings, wenn ich es so schreiben:

double igst = 10; 
double amt = 42; 
double igst_amt = (igst/100) * (amt); 

Console.WriteLine(igst_amt); 

Dann bekomme ich 4.2.

Ich vermute, dass igst tatsächlich eine ganze Zahl ist und Sie Ganzzahlarithmetik ausführen, die in 0 resultieren wird.

sollten Sie igst zu double ändern oder Sie können dies tun:

int igst = 10; 
double amt = 42; 
double igst_amt = igst * amt/100; 

Console.WriteLine(igst_amt); 

Eine einfache Änderung der Berechnung und es funktioniert.

Auch, als eine Randnotiz, scheinen Sie Geldberechnungen mit double zu tun, aber Sie sollten decimal verwenden, wie es für Geld Berechnungen ausgelegt ist.

Verwandte Themen