2016-08-03 8 views
1

文字列として変換せずにデカールをどのように保持しますか?私はすべてのアイデアは私はあなたが整数として保存している小数点以下は四捨五入ですが、小数点以下を保持する

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

}  
+0

を使用し、小数点以下の特定の数に値を四捨五入します。整数は整数だけをサポートするのに対して、 'double'は浮動小数点数をサポートします。 – Bobby

+2

あなたはMath.Round()を試しましたか?関数は入力として小数の値と数を取得します。 –

+0

回答を受け入れてください –

答えて

0

を割り当てる役立つだろう

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

} 
    clientearningscic.ValueChanged += sumEarnings; 

合計をトリガする値に変更イベントを使用していて、テキストにそれをcahngeする必要がありますする必要はいけません。整数は-2,147,483,648から2,147,483,647までの整数をサポートします。しかし、小数値をサポートしていません。

小数値を使用するには、intは使用できませんが、floatまたはdoubleを使用できます。

2

テキスト値をInt16データ型に変換すると、小数点以下の桁数は許可されません。これらをdouble、decimal、またはfloatに変換してみてください。

`int`としてではなく、` double`代わりとしてそれを保存しないでください

Math.Round(decimal val, # of decimals); 
関連する問題