Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 499245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:57:57+00:00 2026-05-13T05:57:57+00:00

I think it is not complicated but after doing some research I can’t find

  • 0

I think it is not complicated but after doing some research I can’t find an answer to a simple problem.

I am iterating through keys in a dictionary and I would like to use the key which is a string as a double in some calculation.

If I do this :

foreach (KeyValuePair<string, List<string> price in dictionary)
double ylevel = Convert.ToDouble(price.Key);

It seems to not work and I get a “Input string was not in a correct format” error.

What is the right way to get a double from the key..

Thanks

Bernard

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:57:57+00:00Added an answer on May 13, 2026 at 5:57 am

    You’re doing it correctly.

    The error message indicates that one of your keys is not actually a double.

    If you step through this example in a debugger, you’ll see it fails on the second item:

    var dictionary = new Dictionary<string, List<string>>();
    dictionary.Add("5.72", new List<string> { "a", "bbb", "cccc" });
    dictionary.Add("fifty two", new List<string> { "a", "bbb", "cccc" });
    
    foreach (KeyValuePair<string, List<string>> price in dictionary)
    {
        double ylevel = Convert.ToDouble(price.Key);
    }
    

    Solution

    To resolve this problem, you should use the following code:

    var dictionary = new Dictionary<string, List<string>>();
    dictionary.Add("5.72", new List<string> { "a", "bbb", "cccc" });
    dictionary.Add("fifty two", new List<string> { "a", "bbb", "cccc" });
    
    foreach (KeyValuePair<string, List<string>> price in dictionary)
    {
        double ylevel;
        if(double.TryParse(price.Key, out ylevel))
        {
            //do something with ylevel
        }
        else
        {
            //Log price.Key and handle this condition
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Excuse the poor title, i can not think of the correct term. I have
I'm using guidelines right now, but I do not think there is any way
I want to make something that makes much sense, but I think its not
I think it's not possible, but I want to make sure.
I've read Visual VM remotely over ssh but I think I've not fully understood
I've made a working regexp, but i think it's not the best use-case: el
I've found many similar questions but nothing with quite the answer I'm after. Part
I want to do something which is not complicated but I do not manage
My fellow programmers did not think to add timestamps to every tables of our
I think that is not easy to understand what I need reading title, so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.