十进制舍入但保持小数

时间:2016-08-03 12:28:19

标签: c#

如何在不将其转换为字符串的情况下将其保留为decmail。我不想将其改为文本,因为我正在使用值更改事件来触发总和

 if (!IsPostBack)
 {        
    clientearningscic.Value = (decimal) _dal.getiandEData(_myuser.id, "clientearningscic") ;
    otherincomeonecic.Text = _dal.getiandEData(_myuser.id, "otherincomethreecic").ToString("n2");

 }
   clientearningscic.ValueChanged += sumEarnings;

任何想法都会帮助我分配

private void sumEarnings(object sender, EventArgs e)
{
        int total = Convert.ToInt16(clientearningscic.Text) + Convert.ToInt16(partnerearningscic.Text) + Convert.ToInt16(incomesupportcic.Text) + Convert.ToInt16(childtaxcreditcic.Text) + Convert.ToInt16(workingtaxcreditcic.Text) + Convert.ToInt16(pensioncic.Text) + Convert.ToInt16(childbenefitcic.Text) + Convert.ToInt16(chidlmaintenancecic.Text) + Convert.ToInt16(nondependantscontributioncic.Text) + Convert.ToInt16(otherincomeonecic.Text) + Convert.ToInt16(otherincometwocic.Text) + Convert.ToInt16(otherincomethreecic.Text);

        lblTotalIcome.Text = total.ToString();

        calcTotal();

}     

2 个答案:

答案 0 :(得分:2)

将文本值转换为Int16数据类型将不允许任何小数。尝试将它们转换为double,decimal或float。

要将值四舍五入到一定数量的小数,请使用

span

答案 1 :(得分:0)

您将其保存为整数。整数支持从-2,147,483,648到2,147,483,647的整数。但他们不支持小数值。

要使用小数值,您无法使用int,但可以使用floatdouble