【问题标题】:Is it possible to check if a value is an int in C#? [duplicate]是否可以检查一个值是否是 C# 中的 int? [复制]
【发布时间】:2015-09-14 00:49:22
【问题描述】:

嘿,所以我目前正在与我的一个朋友合作开发一款游戏,我们正在努力降低我们 AI 的移动性。看到我目前对 C# 不流利,我想知道是否有办法检查一个值是否可以被视为一个 int(如整数)。我想使用它的代码在这里:

while(transform.position != int){
            //Does the actual visual moving
            transform.position = Vector2.MoveTowards (transform.position, monsterPosition, Time.deltaTime * speed);
    }

感谢您提供的任何帮助!

【问题讨论】:

  • position 是什么类型? dynamic?一个可能是整数也可能不是整数的浮点数?

标签: c#


【解决方案1】:

您可以使用 % mod 运算符,查看是否有余数:

if(transform.position % 1 == 0)
{
    //It's a whole number
}

【讨论】:

  • 非常感谢,我完全忘记了这个操作。我会责怪啤酒。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-24
  • 2011-05-21
  • 1970-01-01
  • 2018-07-18
  • 2018-01-13
  • 1970-01-01
相关资源
最近更新 更多