简体   繁体   中英

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

I am writing an Android app using C# and mono. I need to take a string from one of the activities (Android forms) and convert it into a double. When I do this I get very strange results. For example the double.parse in the below (unfinished) method:

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". This is in Visual Studio 2010, with mono for 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? Try testing it for equality with "2". Does it have whitespace around it, or is it of some oddball length? ("2" should be one char long, after all.)

What do you get if you run double.Parse("2"), with the string literal?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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