添加两个十进制值并将最终值转换为INT

时间:2017-09-20 06:49:38

标签: c# int type-conversion decimal

我正在添加两个十进制值,如

decimal one=0; decimal two=0; decimal sum=0;
sum = one+ two;
Int final =0;

示例

sum = 1.2 + 2.2;
sum = 3.4;

现在我想通过忽略小数部分(.4)将此3.4保存在Integer Final中。我怎么能这样做?

3 个答案:

答案 0 :(得分:1)

class Program
    {
        static void Main(string[] args)
        {
            decimal one = 1.4M; decimal two = 3.4M; decimal sum = 0;
            sum = one + two;
            Int32 final = (Int32)(sum);
            Int32 roundfinal = (Int32)(Math.Round(sum));
            Console.WriteLine("final is "+ final);
            Console.WriteLine("roundfinal is " + roundfinal);
            Console.ReadLine();

        }
    }

检查没有圆形轮的答案,圆数4.8是4而没有圆形,4.8轮是5

答案 1 :(得分:0)

您在寻找什么!:

final = Convert.ToInt32(sum);

final = (int)sum;

答案 2 :(得分:0)

Decimal value = Decimal.Add(a1, a2);
FinalOutout int = Convert.ToInt32(value);

它将起作用。