-1

I have the examples like ten percent , thirteen percent I have to convert these to 10% and 13% respectively .

How can I do that , I have to match this string from the long string and need to convert it to numeric , I have following code which is able to find

10 % and 10 percent how can I find Ten percentage and convert it to numeric

Code

public StringBuilder extractPercentage(string html)
{
    StringBuilder formattedString = new StringBuilder();
    foreach (Match m in Regex.Matches(html, @"\d+\%|\s\bpercent\b)"))
    {
        var a = m.Value;
        formattedString.Append(m.Value + "<br/>");
    }
    return formattedString;
}

Result:

100% -- TRUE

100 Percent TRUE

I have two scenarios here recognising amount with having percent with it and converting to numeric

Community
  • 1
  • 1
DotNetDevs
  • 27
  • 5
  • I don't think that's a regex task. You'd probably need something like a `Dictionary` for this. Have a look at [this question](https://stackoverflow.com/q/11278081/4934172). – 41686d6564 stands w. Palestine Aug 06 '18 at 13:51
  • 1
    you can look at this link where you can find how to convert word to number (int). https://stackoverflow.com/questions/11278081/convert-words-string-to-int after this code implementation which you can find on this thread you can replace the strings like this content.Replace("percent","%"); – AlexiAmni Aug 06 '18 at 13:57
  • 1
    Possible duplicate of [Convert words (string) to Int](https://stackoverflow.com/questions/11278081/convert-words-string-to-int) – BartoszKP Aug 06 '18 at 14:02
  • @BartoszKP its not duplicate read twice please – DotNetDevs Aug 06 '18 at 14:04
  • @DotNetDevs If you have two different tasks, than you should ask two separate questions. It's not clear what are you having trouble with. If changing words to numbers than look at the linked duplicate. If changing the word "percent" to "%" is problematic than you have to carefully explain what is so hard about a simple string replacement. – BartoszKP Aug 06 '18 at 14:15
  • @BartoszKP I have edited my question please take a look , I agree what you are saying ,, I need just recognising of the amount of percent preceding with word percent – DotNetDevs Aug 06 '18 at 14:18
  • @DotNetDevs Your question was fine as is. I rolled it back for you. Now, as others have said, you need to look at the linked question to know how to convert words to numbers. Then you can use string replacement to replace words to number **and** "percent" to "%". You can't just say I don't care about the other question because I have additional requirements. Use what's there to finish the first part, and then if you don't know how to proceed from there, you can ask specifically about that. As I said in the first comment above, this can't be done by only using regex. – 41686d6564 stands w. Palestine Aug 06 '18 at 14:27
  • @AhmedAbdelhameed No, it's not "fine as is". As you admit yourself in the next part of your comment :-) – BartoszKP Aug 07 '18 at 08:23

1 Answers1

0

here is a code that should do what you are looking for:

  static string ParseEnglish(string number)
    {

        number= number.Replace("percent", "").Trim();

        string[] words = number.ToLower().Split(new char[] { ' ', '-', ',' }, StringSplitOptions.RemoveEmptyEntries);
        string[] ones = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
        string[] teens = { "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
        string[] tens = { "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
        Dictionary<string, int> modifiers = new Dictionary<string, int>() {
    {"billion", 1000000000},
    {"million", 1000000},
    {"thousand", 1000},
    {"hundred", 100}
};

        if (number == "eleventy billion")
            return int.MaxValue.ToString(); // 110,000,000,000 is out of range for an int!

        int result = 0;
        int currentResult = 0;
        int lastModifier = 1;

        foreach (string word in words)
        {
            if (modifiers.ContainsKey(word))
            {
                lastModifier *= modifiers[word];
            }
            else
            {
                int n;

                if (lastModifier > 1)
                {
                    result += currentResult * lastModifier;
                    lastModifier = 1;
                    currentResult = 0;
                }

                if ((n = Array.IndexOf(ones, word) + 1) > 0)
                {
                    currentResult += n;
                }
                else if ((n = Array.IndexOf(teens, word) + 1) > 0)
                {
                    currentResult += n + 10;
                }
                else if ((n = Array.IndexOf(tens, word) + 1) > 0)
                {
                    currentResult += n * 10;
                }
                else if (word != "and")
                {
                    throw new ApplicationException("Unrecognized word: " + word);
                }
            }
        }
        int ReturnValue = result + currentResult * lastModifier;
        return ReturnValue.ToString () + "%";
    }

This answer is a modification of an answer from this post: Convert words (string) to Int

MorenajeRD
  • 849
  • 1
  • 11
  • 27