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 3348084
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:29:58+00:00 2026-05-18T01:29:58+00:00

Are there any C# libraries out there that provide the same kind of functionality

  • 0

Are there any C# libraries out there that provide the same kind of functionality google does when you type in a query such as “13 miles 743 yards in meters” it will return “21 600 meters” (for example).

What I want to be able to do is give a function the string part 13 miles 743 yards and it spits back an int/double with the given distance in meters. It needs to be able to handle all unit input types (kilometers/meters/furlongs/miles/yards/…) but the output only has to be in meters.

It isn’t that hard to write my own, but it would be great to just have a tested library ready to go.

  • 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-18T01:29:59+00:00Added an answer on May 18, 2026 at 1:29 am

    I couldn’t find any answer to this, so I built my own 🙂 The only real ‘magic’ here is the Regex expression to grab the groups of values/units out of the original string. From there it’s simple fraction/number parsing and then working out how many meters each unit represents. I have not tested this much at all, so please let me know if you find improvements or bugs (the code below should throw an exception when it can’t handle a situation).

    It won’t handle stupid user input, but provided the format of each section is “[number] [unit]” I think it should work fine. There is not much you can assume if the input doesn’t conform (e.g., 12/32/43 or 1.43.3.2.44 as a value) anyway. I think it will handle extra fluff in the sentence too such as 1 kilometer and 10 miles (will strip out the and). I haven’t added every unit possible, if you know of a complete list of units & there meter equivalent I would love to know about it.

    Here are a couple tests,

    var a = ExtractDistance("1 1/16 Miles 3/4 yards");
    var b = ExtractDistance("02234890234.853 meters");
    var c = ExtractDistance("1.8 miles 3.2 furlong");
    var d = ExtractDistance("1 kilometer");
    var e = ExtractDistance("1/16 Miles");
    

    and here is my code:

    private static Dictionary<string, double> _DistanceLookup = new Dictionary<string, double>()
    {
      {"mile", 1609.344},
      {"furlong", 201.168},
      {"yard", 0.9144},
      {"inch", 0.0254},
      {"foot", 0.3048},
      {"feet", 0.3048},
      {"kilometer", 1000},
      {"kilometre", 1000},
      {"metre", 1},
      {"meter", 1},
      {"centimeter", 0.01},
      {"centimetre", 0.01},
      {"millimeter", 0.001},
      {"millimetre", 0.001},
    };
    
    private static double ConvertFraction(string fraction)
    {
      double value = 0;
      if (fraction.Contains('/'))
      {
        // If the value contains /, we need to work out the fraction
        string[] splitVal = fraction.Split('/');
        if (splitVal.Length != 2)
        {
          ScrewUp(fraction, "splitVal.Length");
        }
    
        // Turn the fraction into decimal
        value = double.Parse(splitVal[0]) / double.Parse(splitVal[1]);
      }
      else
      {
        // Otherwise it's a simple parse
        value = double.Parse(fraction);
      }
      return value;
    }
    
    public static double ExtractDistance(string distAsString)
    {
      double distanceInMeters = 0;
      /* This will have a match per unit type.
       * e.g., the string "1 1/16 Miles 3/4 Yards" would have 2 matches
       * being "1 1/16 Miles", "3/4 Yards".  Each match will then have 4
       * groups in total, with group 3 being the raw value and 4 being the
       * raw unit
       */
      var matches = Regex.Matches(distAsString, @"(([\d]+[\d\s\.,/]*)\s([A-Za-z]+[^\s\d]))");
      foreach (Match match in matches)
      {
        // If groups != 4 something went wrong, we need to rethink our regex
        if (match.Groups.Count != 4)
        {
          ScrewUp(distAsString, "match.Groups.Count");
        }
        string valueRaw = match.Groups[2].Value;
        string unitRaw = match.Groups[3].Value;
    
        // Firstly get the value
        double value = 0;
        if (valueRaw.Contains(' '))
        {
          // If the value contains /, we need to work out the fraction
          string[] splitVal = valueRaw.Split(' ');
          if (splitVal.Length != 2)
          {
            ScrewUp(distAsString, "splitVal.Length");
          }
    
          // Turn the fraction into decimal
          value = ConvertFraction(splitVal[0]) + ConvertFraction(splitVal[1]);
        }
        else
        {
          value = ConvertFraction(valueRaw);
        }
    
        // Now work out based on the unit type
        // Clean up the raw unit string
        unitRaw = unitRaw.ToLower().Trim().TrimEnd('s');
    
        if (!_DistanceLookup.ContainsKey(unitRaw))
        {
          ScrewUp(distAsString, "unitRaw");
        }
        distanceInMeters += value * _DistanceLookup[unitRaw];
      }
      return distanceInMeters;
    }
    
    private static void ScrewUp(string val, string prop)
    {
      throw new ArgumentException("Extract distance screwed up on string [" + val + "] (bad " + prop + ")");
    }
    

    Enjoy! I hope someone out there finds this useful. Please leave comments/suggestions.

    EDIT: added a , to the regex string to handle 1,300 meters style format

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there any libraries out there for Java that will accept two strings, and
Are there any publicly available libraries or APIs out there on Ubuntu that allow
Is there any libraries that would allow me to use the same known notation
Are there any JavaScript libraries out there that allow you to create heatmaps using
Are there any libraries out there (preferably a self contained Text Edit Control) for
Are there any UPS WorldShip integration APIS or Libraries out there for .net? I've
Are there any libraries, pieces of code or suchlike that'll let me play ZX
Are there any libraries that can take a few digital pictures of an object
are there any libraries out there to minimize the differences of the behavior between
Are there any libraries (3rd party or built-in) in PHP to calculate text diffs?

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.