2010-02-02 113 views
1

在我的銷售詳細軟件中,我想計算TotalCost,Discount,NettotalCost。一旦從文本框輸入質量和比率,所有值都應該自動填充。在我的程序中,它能夠顯示TotalCost和NetTotal,但沒有顯示折扣價值,它總是隻顯示0。這裏是我的代碼,請人修改它有什麼錯在這裏....在BAL類邏輯中需要幫助

public class SalesEntity 
{ 
    private string custid; 
    private string custname; 
    private string productname; 
    private int quantity; 
    private float rate; 
    private float total; 
    private float discount; 
    private float NetTotal; 

    public string CUSTOMERID 
    { 
     get 
     { 
      return custid; 
     } 
     set 
     { 
      custid = value; 
     } 
    } 
    public string CUSTOMERNAME 
    { 
     get 
     { 
      return custname; 
     } 
     set 
     { 
      custname = value; 
     } 
    } 
    public string PRODUCTNAME 
    { 
     get 
     { 
      return productname; 
     } 
     set 
     { 
      productname = value; 
     } 
    } 
    public int QUANTITY 
    { 
     get 
     { 
      return quantity; 
     } 
     set 
     { 
      quantity = value; 
     } 
    } 
    public float RATE 
    { 
     get 
     { 
      return rate; 
     } 
     set 
     { 
      rate = value; 
     } 
    } 
    public float TOTAL 
    { 
     get 
     { 
      return total; 
     } 
     set 
     { 
      total = value; ; 

     } 
    } 
    public float DISCOUNT 
    { 
     get 
     { 
      return discount; 
     } 
     set 
     { 
      discount = value; 
     } 
    } 
    public float NETTOTAL 
    { 
     get 
     {    
      return NetTotal; 
     } 
     set 
     { 
      NetTotal = value; 
     } 
    } 
} 
public class SalesBALManager 
{ 
    public SalesEntity Compute(SalesEntity salesEntity) 
    { 
     salesEntity.TOTAL = salesEntity.QUANTITY * salesEntity.RATE; 
     salesEntity.DISCOUNT = (10/100 * salesEntity.TOTAL); 
     salesEntity.NETTOTAL = salesEntity.TOTAL - salesEntity.DISCOUNT; 
     return salesEntity; 

    } 
} 
protected void TxtRate_TextChanged(object sender, EventArgs e) 
    { 
     if (Page.IsPostBack) 
     { 
      SalesBALManager obj = new SalesBALManager(); 
      SalesEntity salesentity = new SalesEntity(); 

      salesentity.QUANTITY = Convert.ToInt32(TxtQuantity.Text); 
      salesentity.RATE = Convert.ToInt32(TxtRate.Text); 
      salesentity.CUSTOMERID = TxtCustId.Text; 
      salesentity.CUSTOMERNAME = TxtCustName.Text; 

      salesentity = obj.Compute(salesentity); 

      TxtTotal.Text = salesentity.TOTAL.ToString(); 
      TxtDiscount.Text = salesentity.DISCOUNT.ToString();    
      TxtNetTotal.Text = salesentity.NETTOTAL.ToString(); 
     } 

    } 

回答

1

懷疑10/100正在使用整數除法,給人0計算,則正在施放的乘法。爲什麼不只是替代0.1

2

您至少有兩個問題。首先,當你使用十進制和秒時,你使用的是浮點數,除以10/100時,你正在使用整數算術。使用整數算術時,結果爲零。我會更改浮點數到小數點,並指定0.1M而不是10/100。我還會更加註意我的字符串格式,以便小數點中的小數點數是固定的,例如discount.ToString("0.00")

+0

嗨先生tvanfosson,謝謝你的回覆現在工作正常... – sumit 2010-02-02 12:47:50

1

10/100是整數除法,返回0的整數。將它們轉換爲浮點數執行浮點除法。

((float)10/(float)100)