在 C# 中检查 double 是否为整数的好方法是什么?

发布于 2024-09-30 10:19:34 字数 446 浏览 3 评论 0原文

可能的重复:
如何确定小数/双精度数是否为整数?

我有一个 double 类型的变量,我想检查它是否是整数。

目前我有

public bool CheckIfInteger(double number)
{
    return number.ToString().Contains(".") == false;
}

没有更好的办法?

更新:抱歉,我没有意识到可能会造成混淆,我所说的整数是指整数的数学定义,即自然数以及非零自然数的负数。

Possible Duplicate:
How to determine if a decimal/double is an integer?

I have a variable of type double and am wanting to check whether it is an integer.

At the moment I have

public bool CheckIfInteger(double number)
{
    return number.ToString().Contains(".") == false;
}

Is there a better way?

UPDATE: Sorry I didn't realise the potential for confusion, by integer I meant the mathematical definiton of integer, that is the natural numbers together with the negatives of the non-zero natural numbers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

〆凄凉。 2024-10-07 10:19:34
return Math.Truncate(number) == number;

正如评论中提到的,您可能需要考虑这样一个事实:数字的双精度表示可能不是精确的整数。在这种情况下,您需要考虑一些误差范围:

double diff = Math.Abs(Math.Truncate(number) - number);
return (diff < 0.0000001) || (diff > 0.9999999);
return Math.Truncate(number) == number;

As mentioned in the comments, you might need to take account of the fact that a double representation of your number might not be an exact integer. In that case you'll need to allow for some margin-of-error:

double diff = Math.Abs(Math.Truncate(number) - number);
return (diff < 0.0000001) || (diff > 0.9999999);
ヤ经典坏疍 2024-10-07 10:19:34

如果您想检查 Integer,可以这样做:

public bool IsInteger(double number)
{
    return (number % 1 == 0);
}

如果您还想检查该数字是否可以转换为 Int32:

public bool IsInt32(double number)
{
    return (number % 1 == 0) && number >= Int32.MinValue && number <= Int32.MaxValue;
}

If you want to check for an Integer, this will do it:

public bool IsInteger(double number)
{
    return (number % 1 == 0);
}

If you additionally want to check if the number could be converted into an Int32:

public bool IsInt32(double number)
{
    return (number % 1 == 0) && number >= Int32.MinValue && number <= Int32.MaxValue;
}
怂人 2024-10-07 10:19:34

使用字符串表示的陷阱是所使用的区域设置,并且您必须处理舍入问题。大多数情况下0.999999999可以被认为是整数1。以下是考虑舍入误差的小片段:

Math.Abs(number - Math.Round(number)) < EPSILON

其中 EPSILON 是一个足够小的双精度值,例如 0.00001

另请参阅此内容以获取更多信息:http://msdn.microsoft.com/en-us/library/system.double.epsilon.aspx

The pitfall of working with string representations is the locale used and yoou have to take care of rounding issues. 0.999999999 can be considered to be integer 1 in most cases. Here is a small snippet taking into account rounding errors:

Math.Abs(number - Math.Round(number)) < EPSILON

where EPSILON is a double value that is small enough for your purpose 0.00001 for example

See also this for some more information: http://msdn.microsoft.com/en-us/library/system.double.epsilon.aspx

番薯 2024-10-07 10:19:34

尝试:

public bool CheckIfInteger(double number)
{
    return ((double) (int) number == number);
}

或者更漂亮的:

public bool CheckIfInteger(double number)
{
    return (Math.Floor(number) == number);
}

Try:

public bool CheckIfInteger(double number)
{
    return ((double) (int) number == number);
}

Or the prettier:

public bool CheckIfInteger(double number)
{
    return (Math.Floor(number) == number);
}
记忆消瘦 2024-10-07 10:19:34

我会使用 TryParse

  double value = 2.0;
  int number;
  bool result = Int32.TryParse(value.ToString(), out number);

I'd use TryParse:

  double value = 2.0;
  int number;
  bool result = Int32.TryParse(value.ToString(), out number);
奈何桥上唱咆哮 2024-10-07 10:19:34
    public static bool CheckIfInteger(double number)
    {
        return number - Math.Truncate(number) == 0;
    }
    public static bool CheckIfInteger(double number)
    {
        return number - Math.Truncate(number) == 0;
    }
微暖i 2024-10-07 10:19:34

我认为更好的问题是:如何判断 double 是否足够接近整数以被视为整数?因为否则的话,你一定会遇到歧义。所以我推荐这样的东西:

return Math.Abs(someDouble - Math.Round(someDouble)) < TOLERANCE;

I think that a better question is: How can I tell if a double is close enough to an integer to be considered an integer for my purposes? Because otherwise, you are bound to run into ambiguities. So I'd recommend something like this:

return Math.Abs(someDouble - Math.Round(someDouble)) < TOLERANCE;
似最初 2024-10-07 10:19:34

我喜欢 abatishchev 使用 CurrentCulture 的想法。

return number.ToString().Contains(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator) == false;

这是否不能解决 epsilon 问题(我最初甚至没有考虑过)?

I'm liking abatishchev's idea to use CurrentCulture.

return number.ToString().Contains(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator) == false;

Would this not solve the epsilon problem (which I didn't even consider initially)?

九命猫 2024-10-07 10:19:34

我使用以下字符串扩展方法来检查正则表达式模式

   public static bool IsInteger(this string inputString)
    {
        Regex regexInteger = new Regex(@"^[-]?\d+$");
        Match m = regexInteger.Match(inputString);
        return m.Success;
    }

I use the following string extension method which checks against a RegEx pattern

   public static bool IsInteger(this string inputString)
    {
        Regex regexInteger = new Regex(@"^[-]?\d+$");
        Match m = regexInteger.Match(inputString);
        return m.Success;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文