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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:45:54+00:00 2026-05-22T19:45:54+00:00

I’m writing a function to take shorthand values and convert them into a standardized

  • 0

I’m writing a function to take shorthand values and convert them into a standardized numeric format. Is there any standard code out there that would do “best possible” conversion of arbitrary measurement text and turn it into numeric measurements if the text is valid?

I guess I’m looking for something like bool TryParseMeasurement(string s, out decimal d). Does anyone know of a function like this?

Here’s an example of some of the input values I’ve seen:

Imperial

  • 6 inches
  • 6in
  • 6”
  • 4 feet 2 inches
  • 4’2”
  • 4 ‘ 2 “
  • 3 feet
  • 3’
  • 3 ‘
  • 3ft
  • 3ft10in
  • 3ft 13in (should convert to 4’1”)

Metricc

  • 1m
  • 1.2m
  • 1.321m
  • 1 meter
  • 481mm
  • 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-22T19:45:55+00:00Added an answer on May 22, 2026 at 7:45 pm

    Here’s some code we wrote in an app quite some time ago, where we were doing something similar. It’s not the best, but you may be able to adapt, or get some sort of jumping off point.

    public static class UnitConversion
    {
        public static string[] lstFootUnits = new string[] {"foots", "foot", "feets", "feet", "ft", "f", "\""};
        public static string sFootUnit = "ft";
    
        public static string[] lstInchUnits = new string[] { "inches", "inchs", "inch", "in", "i", "\'" };
        public static string sInchUnit = "in";
    
        public static string[] lstPoundUnits = new string[] { "pounds", "pound", "pnds", "pnd", "lbs", "lb", "l", "p" };
        public static string sPoundUnit = "lbs";
    
        public static string[] lstOunceUnits = new string[] { "ounces", "ounce", "ozs", "oz", "o" };
        public static string sOunceUnit = "oz";
    
        public static string[] lstCentimeterUnits = new string[] { "centimeters", "centimeter", "centimetres", "centimetre", "cms", "cm", "c"};
        public static string sCentimeterUnit = "cm";
    
        public static string[] lstKilogramUnits = new string[] { "kilograms", "kilogram", "kilos", "kilo", "kgs", "kg", "k" };
        public static string sKilogramsUnit = "kgs";
    
        /// <summary>
        /// Attempt to convert between feet/inches and cm
        /// </summary>
        /// <param name="sHeight"></param>
        /// <returns></returns>
        public static string ConvertHeight(string sHeight)
        {
            if (!String.IsNullOrEmpty(sHeight))
            {
                sHeight = UnitConversion.CleanHeight(sHeight);
    
                if (sHeight.Contains(UnitConversion.sFootUnit))
                {
                    sHeight = sHeight.Replace(UnitConversion.sFootUnit, "|");
                    sHeight = sHeight.Replace(UnitConversion.sInchUnit, "|");
    
                    string[] sParts = sHeight.Split('|');
    
                    double? dFeet = null;
                    double? dInches = null;
                    double dFeetParsed;
                    double dInchesParsed;
    
                    if (sParts.Length >= 2 && double.TryParse(sParts[0].Trim(), out dFeetParsed))
                    {
                        dFeet = dFeetParsed;
                    }
                    if (sParts.Length >= 4 && double.TryParse(sParts[2].Trim(), out dInchesParsed))
                    {
                        dInches = dInchesParsed;
                    };
    
                    sHeight = UnitConversion.FtToCm(UnitConversion.CalculateFt(dFeet ?? 0, dInches ?? 0)).ToString() + " " + UnitConversion.sCentimeterUnit;
                }
                else if (sHeight.Contains(UnitConversion.sCentimeterUnit))
                {
                    sHeight = sHeight.Replace(UnitConversion.sCentimeterUnit, "|");
                    string[] sParts = sHeight.Split('|');
    
                    double? dCentimeters = null;
                    double dCentimetersParsed;
    
                    if (sParts.Length >= 2 && double.TryParse(sParts[0].Trim(), out dCentimetersParsed))
                    {
                        dCentimeters = dCentimetersParsed;
                    }
    
                    int? iFeet;
                    int? iInches;
                    if (UnitConversion.CmToFt(dCentimeters, out iFeet, out iInches))
                    {
                        sHeight = (iFeet != null) ? iFeet.ToString() + " " + UnitConversion.sFootUnit : "";
                        sHeight += (iInches != null) ? " " + iInches.ToString() + " " + UnitConversion.sInchUnit : "";
                        sHeight = sHeight.Trim();
                    }
                    else
                    {
                        sHeight = "";
                    }
                }
                else
                {
                    sHeight = "";
                }
            }
            else
            {
                sHeight = "";
            }
    
            return sHeight;
        }
    
        /// <summary>
        /// Attempt to convert between Kgs and Lbs
        /// </summary>
        /// <param name="sWeight"></param>
        /// <returns></returns>
        public static string ConvertWeight(string sWeight)
        {
            if (!String.IsNullOrEmpty(sWeight))
            {
                sWeight = UnitConversion.CleanWeight(sWeight);
    
                if (sWeight.Contains(UnitConversion.sKilogramsUnit))
                {
                    sWeight = sWeight.Replace(UnitConversion.sKilogramsUnit, "|");
    
                    string[] sParts = sWeight.Split('|');
    
                    double? dKilograms = null;
                    double dKilogramsParsed;
    
                    if (sParts.Length >= 2 && double.TryParse(sParts[0].Trim(), out dKilogramsParsed))
                    {
                        dKilograms = dKilogramsParsed;
                    }
    
                    sWeight = UnitConversion.KgToLbs(dKilograms).ToString("#.###") + " " + UnitConversion.sPoundUnit;
                }
                else if (sWeight.Contains(UnitConversion.sPoundUnit))
                {
                    sWeight = sWeight.Replace(UnitConversion.sPoundUnit, "|");
    
                    string[] sParts = sWeight.Split('|');
    
                    double? dPounds = null;
                    double dPoundsParsed;
    
                    if (sParts.Length >= 2 && double.TryParse(sParts[0].Trim(), out dPoundsParsed))
                    {
                        dPounds = dPoundsParsed;
                    }
    
                    sWeight = UnitConversion.LbsToKg(dPounds).ToString("#.###") + " " + UnitConversion.sKilogramsUnit;
                }
                else
                {
                    sWeight = "";
                }
            }
            else
            {
                sWeight = "";
            }
    
            return sWeight;
        }
    
        public static double? CalculateFt(double dFt, double dInch)
        {
            double? dFeet = null;
            if (dFt >= 0 && dInch >= 0 && dInch <= 12)
            {
                dFeet = dFt + (dInch / 12);
            }
            return dFeet;
        }
    
        public static double KgToLbs(double? dKg)
        {
            if (dKg == null)
            {
                return 0;
            }
            return dKg.Value * 2.20462262;
        }
    
        public static double LbsToKg(double? dLbs)
        {
            if (dLbs == null)
            {
                return 0;
            }
            return dLbs.Value / 2.20462262;
        }
    
        public static double FtToCm(double? dFt)
        {
            if (dFt == null)
            {
                return 0;
            }
            return dFt.Value * 30.48;
        }
    
        public static bool CmToFt(double? dCm, out int? iFt, out int? iInch)
        {
    
            if (dCm == null)
            {
                iFt = null;
                iInch = null;
                return false;
            }
    
            double dCalcFeet = dCm.Value / 30.48;
            double dCalcInches = dCalcFeet - Math.Floor(dCalcFeet);
            dCalcFeet = Math.Floor(dCalcFeet);
            dCalcInches = dCalcInches * 12;
    
            iFt = (int)dCalcFeet;
            iInch = (int)dCalcInches;
            return true;
        }
    
        private static string CleanUnit(string sOriginal, string[] lstReplaceUnits, string sReplaceWithUnit)
        {
            System.Text.StringBuilder sbPattern = new System.Text.StringBuilder();
    
            foreach (string sReplace in lstReplaceUnits)
            {
                if (sbPattern.Length > 0)
                {
                    sbPattern.Append("|");
                }
                sbPattern.Append(sReplace);
            }
            sbPattern.Insert(0,@"(^|\s)(");
            sbPattern.Append(@")(\s|$)");
    
    
            System.Text.RegularExpressions.Regex rReplace = new System.Text.RegularExpressions.Regex(sbPattern.ToString(), System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    
            sOriginal = rReplace.Replace(sOriginal, sReplaceWithUnit);
    
            /*foreach (string sReplace in lstReplaceUnits)
            {
    
                sOriginal = sOriginal.Replace(sReplace, " " + sReplaceWithUnit);
            }*/
    
    
            return sOriginal;
        }
    
        private static bool StringHasNumbers(string sText)
        {
            System.Text.RegularExpressions.Regex rxNumbers = new System.Text.RegularExpressions.Regex("[0-9]+");
    
            return rxNumbers.IsMatch(sText);
        }
    
        private static string ReduceSpaces(string sText)
        {
            while (sText.Contains("  "))
            {
                sText = sText.Replace("  ", " ");
            }
    
            return sText;
        }
    
        private static string SeperateNumbers(string sText)
        {
            bool bNumber = false;
            if (!String.IsNullOrEmpty(sText))
            {
                for (int iChar = 0; iChar < sText.Length; iChar++)
                {
                    bool bIsNumber = (sText[iChar] >= '0' && sText[iChar] <= '9') || 
                        (sText[iChar] == '.' && iChar < sText.Length - 1 && sText[iChar + 1] >= '0' && sText[iChar + 1] <= '9');
    
                    if (iChar > 0 && bIsNumber != bNumber)
                    {
                        sText = sText.Insert(iChar, " ");
                        iChar++;
                    }
    
                    bNumber = bIsNumber;
                }
            }
    
            return sText;
        }
    
        public static string CleanHeight(string sHeight)
        {
            if (UnitConversion.StringHasNumbers(sHeight))
            {
                sHeight = SeperateNumbers(sHeight);
                sHeight = CleanUnit(sHeight, UnitConversion.lstFootUnits, UnitConversion.sFootUnit);
                sHeight = CleanUnit(sHeight, UnitConversion.lstInchUnits, UnitConversion.sInchUnit);
                sHeight = CleanUnit(sHeight, UnitConversion.lstCentimeterUnits, UnitConversion.sCentimeterUnit);
                sHeight = SeperateNumbers(sHeight);
                sHeight = ReduceSpaces(sHeight);
            }
            else
            {
                sHeight = "";
            }
    
            return sHeight;
        }
    
        public static string CleanWeight(string sWeight)
        {
            if (UnitConversion.StringHasNumbers(sWeight))
            {
                sWeight = SeperateNumbers(sWeight);
                sWeight = CleanUnit(sWeight, UnitConversion.lstOunceUnits, UnitConversion.sOunceUnit);
                sWeight = CleanUnit(sWeight, UnitConversion.lstPoundUnits, UnitConversion.sPoundUnit);
                sWeight = CleanUnit(sWeight, UnitConversion.lstKilogramUnits, UnitConversion.sKilogramsUnit);
                sWeight = SeperateNumbers(sWeight);
                sWeight = ReduceSpaces(sWeight);
            }
            else
            {
                sWeight = "";
            }
    
            return sWeight;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I am writing an app with both english and french support. The app requests
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.