【问题标题】:Add two Decimal Values and Convert the Final value to INT添加两个十进制值并将最终值转换为 INT
【发布时间】:2018-03-01 02:29:22
【问题描述】:

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

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 中。我该怎么做?

【问题讨论】:

  • 你有没有尝试过?只需将结果分配给int。小数点将被截断
  • 感谢回复

标签: c# int type-conversion decimal


【解决方案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

【讨论】:

  • 这是答案 感谢您的回复,这对我有帮助
【解决方案2】:

你在找什么!:

final = Convert.ToInt32(sum);

或者:

final = (int)sum;

【讨论】:

    【解决方案3】:
    Decimal value = Decimal.Add(a1, a2);
    FinalOutout int = Convert.ToInt32(value);
    

    它会起作用的。

    【讨论】:

      猜你喜欢
      • 2012-10-11
      • 1970-01-01
      • 2015-09-01
      • 1970-01-01
      • 2013-02-06
      • 2014-02-05
      • 1970-01-01
      • 2012-08-08
      相关资源
      最近更新 更多