【问题标题】:how to fix the error on the Round, what should i change the line to?如何修复回合上的错误,我应该将线路更改为什么?
【发布时间】:2016-12-29 10:42:40
【问题描述】:
public void ChangeTexts(long length, int position, int percent, double speed)
        {
            Label3.Text = "File Size: " + Math.Round((length / 1024), 2) + " KB";

严重性代码描述项目文件行抑制状态 错误 CS0121 调用在以下方法或属性之间不明确:'Math.Round(double, int)' 和 'Math.Round(decimal, int)'

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    您当前在整数运算中除以 1024,然后将结果四舍五入。换句话说,你正在四舍五入已经是整数的东西 - 不是很有用。

    最简单的解决方法是除以 1024.0,以使其在 double 算术中发生:

    Label3.Text = "File Size: " + Math.Round((length / 1024.0), 2) + " KB";
    

    或者更好,只需在格式本身内进行:

    Label3.Text = $"File Size: {length / 1024.0:0.00}KB";
    

    【讨论】:

      【解决方案2】:

      错误是不言自明的。您应该指定要使用的类型。这是因为Math.Round() 方法没有long 的重载。

      所以你可以这样做:

       public void ChangeTexts(long length, int position, int percent, double speed)
       {
           Label3.Text = "File Size: " + Math.Round((length / 1024d), 2) + " KB";
       }
      

      现在您使用double 类型进行操作。 dliteraldouble 类型。现在表达式(double)length / 1024d 将返回double。而Math.Round 有过载。

      【讨论】:

      • 现在将在整数算术中执行除法,将该整数转换为小数,并将其四舍五入到小数点后两位。这有什么用?
      • 由于分号,它仍然无法编译,并且您不需要 both 操作数为double。正如我在回答中所说的那样,老实说,我认为让格式化来做到这一点更简单......
      猜你喜欢
      • 2017-06-23
      • 2012-08-01
      • 2018-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-15
      • 2019-07-09
      • 1970-01-01
      相关资源
      最近更新 更多