Convert numbers in string to English word format

Zach

I have a C# method that converts integer numbers into their English word counterparts; however, how can I implement this so it'll work with a string.

For example, if I have the following string:

There was a dog. The dog ate 1000 bones. After eating, he was very sleepy. He slept for 12 hours.

I want it to parse it and return:

There was a dog. The dog ate one thousand bones. After eating, he was very sleepy. He slept for twelve hours.

How would I go by taking the numbers out of the sentence, and using the conversion method (below)?

public static string NumberToWords(int number)
{
    if (number == 0)
        return "zero";

    if (number < 0)
        return "minus " + NumberToWords(Math.Abs(number));

    string words = "";

    if ((number / 1000000) > 0)
    {
        words += NumberToWords(number / 1000000) + " million ";
        number %= 1000000;
    }

    if ((number / 1000) > 0)
    {
        words += NumberToWords(number / 1000) + " thousand ";
        number %= 1000;
    }

    if ((number / 100) > 0)
    {
        words += NumberToWords(number / 100) + " hundred ";
        number %= 100;
    }

    if (number > 0)
    {
        if (words != "")
            words += "and ";

        var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
        var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

        if (number < 20)
            words += unitsMap[number];
        else
        {
            words += tensMap[number / 10];
            if ((number % 10) > 0)
                words += "-" + unitsMap[number % 10];
        }
    }

    return words;
}

I tried doing a foreach, like this, but it didn't work...

var matches = Regex.Matches(myString, "[0-9]+");
foreach(var match in matches)
{
   NumberToWords(match);
}
Selman Genç

You could use Split and LINQ:

var input = "There was a dog. The dog ate 1000 bones. After eating, he was very sleepy. He slept for 12 hours.";

var invalidChars = new [] { ',', ';', '-', '.' };
var words = input.Split(' ')
            .Select(x =>
            {
                if (x.All(char.IsDigit) || x.Trim(invalidChars).All(char.IsDigit))
                    return NumberToWords(int.Parse(x.Trim(invalidChars)));
                return x;
            });

var output = string.Join(" ", words);

Btw I assume that NumberToWord method is working correctly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to convert Persian/Arabic numbers to English numbers in Kotlin?

From Java

How to format numbers as currency string?

From Dev

Convert String to DateTime Format

From Dev

How whether a string is randomly generated or plausibly an English word?

From Dev

String Format with numbers or Space

From Dev

How to format string with numbers and decimals?

From Dev

How to convert english numbers inside a string to Persian/Arabic numbers in Objective-C?

From Dev

Convert English numbers in a String to Arabic numbers

From Dev

Convert the numbers of a set to String

From Dev

Convert English numbers to Persian/Arabic only for a specified div

From Dev

i want to convert arebic numbers to english and i tried code below

From Dev

Convert Arabic String to english number in Swift

From Dev

Javascript: How to transform float numbers in english format to float numbers in spanish format?

From Dev

Convert string to DateTime and format

From Dev

Convert Word 2010 document labels from German to English?

From Dev

Convert a string to a time format

From Dev

Convert string to int with format

From Dev

Convert/encode string to numbers

From Dev

Convert numbers to English strings

From Dev

Convert string value to english word

From Dev

Convert English numbers in a String to Arabic numbers

From Dev

Convert a String format to

From Dev

Convert String to Number with Format

From Dev

Convert the numbers of a set to String

From Dev

Switch Arabic Numbers To English In String

From Dev

how to return numbers in word format from a string in python

From Dev

Convert Number into english Word-Meaning using XSLT

From Dev

Convert string a double in a word macro

From Dev

Check if a string format a word

Related Related

HotTag

Archive