【问题标题】:C# Double division returning infinity when is not supposed toC# 双除法不应该返回无穷大
【发布时间】:2017-10-18 02:56:06
【问题描述】:

我在条目完成事件上有此代码:(Xamarin)

if (double.TryParse(txt_MinRate.Text, out double i))
{
    if (i < 0.0 || i > 6.0)
    {
        if (i > 6.0)
        {
            DisplayAlert("Invalid input", "6% is the maximum rate allowed.", "OK");
            txt_MinRate.Text = "6.0";
        }
        else if (i < 0.0)
        {
            DisplayAlert("Invalid input", "Don't worry. There will never be a negative rate", "OH Good");
            txt_MinRate.Text = "0.0";
        }
    }
    else
    {
        Settings.MinRate = i / 100.0;
    }
}

问题是...如果我输入 0.01 Settings.MinRate 保存值为 0.0001,这是预期会发生的。

如果我在条目中键入 0.001.... Settings.MinRate 与 -Infinity 一起保存.....

谁能解释这里发生了什么?

编辑:

公平地说,Settings.MinRate 并不是一个简单的双倍...我认为... 这是在 Settings.cs 文件中:

        private const string txt_MinRateyKey = "txt_MinRate_Key";
        private static readonly double txt_MinRateDefault = 0.0;
        public static double MinRate
        {
            get => AppSettings.GetValueOrDefault(txt_MinRateyKey, txt_MinRateDefault);
            set => AppSettings.AddOrUpdateValue(txt_MinRateyKey, value);
        }

编辑 2:

我只是添加了一个简单的行:double x = i / 100;就在 Settings.MinRate = i / 100;

抱歉上次编辑...我的错...结果对于双 x 值是正确的...1E-05...所以问题似乎是设置插件...

【问题讨论】:

  • Settings.MinRate 是什么类型?
  • 它也是双倍的。我用回复编辑了我的问题
  • 当您说“已保存”时,您是指使用设置插件实际保存/加载它的时间,还是实际完成 MinRate 计算的时间?
  • 就是断点计算后的结果 Settings.MinRate = i / 100.0;

标签: android xamarin division cultureinfo infinity


【解决方案1】:

问题是我正在使用的设置插件...

我在插件 github 上创建了错误报告,并很快在插件的 3.1.1 版本中修复。

它现在可以按预期工作了! :)

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多