简体   繁体   English

适用于Android的C#Mono-double.parse给出了奇怪的结果

[英]C# Mono for Android - double.parse is giving weird results

I am writing an Android app using C# and mono. 我正在使用C#和mono编写一个Android应用程序。 I need to take a string from one of the activities (Android forms) and convert it into a double. 我需要从其中一个活动(Android表单)中获取一个字符串,并将其转换为双精度形式。 When I do this I get very strange results. 当我这样做时,我会得到非常奇怪的结果。 For example the double.parse in the below (unfinished) method: 例如,以下(未完成)方法中的double.parse:

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        try
        {
            //add / edit the task
            if (resultCode == Result.Ok)
            {
                if (m_isAddMode)
                {

                    var tliTemplateControl = new TemplateControlTaskListItem();
                    tliTemplateControl.Code = data.GetStringExtra("selectedtask");
                    tliTemplateControl.Description = data.GetStringExtra("selecteddescription");
                    tliTemplateControl.RequiredQty = double.Parse(data.GetStringExtra("qty"));


                    m_taskList.Items.Add(tliTemplateControl);

                    this.ListAdapter = new TaskListAdapter(this, m_taskList.Items);
                }
                else
                {

                }
            }
        }
        catch (Exception ex)
        {

        }
    }

returns -1.03054917417467E-05 when the text being parsed is "2". 当要分析的文本为“ 2”时,返回-1.03054917417467E-05。 This is in Visual Studio 2010, with mono for android 4.2.3. 这在Visual Studio 2010中,适用于Android 4.2.3。 Has anyone got any ideas? 有人知道吗? Thank you. 谢谢。

Divide things up more finely. 将事情更精细地划分。 Store your intermediate results in variables. 将中间结果存储在变量中。 Do one thing per statement-- don't chain the result of one calculation into another calculation. 对每个语句做一件事-不要将一个计算的结果链接到另一计算。 Use your debugger. 使用调试器。 Inspect what happens at every step. 检查每一步会发生什么。

What string DOES data.GetStringExtra("qty") return, really? 实际上,data.GetStringExtra(“ qty”)返回什么字符串? Try testing it for equality with "2". 尝试使用“ 2”测试其是否相等。 Does it have whitespace around it, or is it of some oddball length? 它周围是否有空格,或者它的长度有些奇怪? ("2" should be one char long, after all.) (毕竟,“ 2”应为一个字符长。)

What do you get if you run double.Parse("2"), with the string literal? 如果使用字符串字面量运行double.Parse(“ 2”),会得到什么?

At some point, you will either see what you're doing wrong, or you'll find something that is definitely a compiler or library area. 在某个时候,您要么看到自己在做错什么,要么会发现绝对是编译器或库区域的东西。 When you get to that point, you'll know what to do next. 当您达到这一点时,您将知道下一步该怎么做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM