【问题标题】:Need help in BAL Class logic在 BAL 类逻辑方面需要帮助
【发布时间】:2010-02-02 20:32:13
【问题描述】:

在我的销售明细软件中,我想计算 TotalCost、Discount、NettotalCost。从文本框中输入 Quantity 和 rate 后,应自动填充所有值。在我的程序中,它能够显示 TotalCost 和 NetTotal 但不显示折扣值,它始终只显示 0。这是我的代码,请有人修改它这里有什么问题......

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();
        }

    }

【问题讨论】:

    标签: c# user-interface


    【解决方案1】:

    你至少有两个问题。首先,当您应该使用十进制时使用浮点数,其次,在划分10 / 100 时使用整数算术。使用整数算术时,结果为零。我会将浮点数更改为小数并指定0.1M 而不是10/100。我也会更加小心我的字符串格式,以便十进制数字中的小数点数是固定的,例如discount.ToString( "0.00" )

    【讨论】:

      【解决方案2】:

      怀疑10 / 100 正在使用整数除法计算,得到0,然后被强制转换为乘法。为什么不直接替换0.1

      【讨论】:

        【解决方案3】:

        10/100 是整数除法,返回整数 0。将它们转换为浮点数以执行浮点除法。

        ((float)10/(float)100)
        

        【讨论】:

          猜你喜欢
          • 2013-12-14
          • 2015-10-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多