Humanizer.WordsToNumberExtension
WordsToNumberExtension Class
Transform humanized string to number; e.g. one => 1
public static class WordsToNumberExtension
Inheritance System.Object → WordsToNumberExtension
Methods
WordsToNumberExtension.ToNumber(this string, CultureInfo) Method
Converts a spelled-out number string to its integer representation.
public static int ToNumber(this string words, System.Globalization.CultureInfo culture);
Parameters
words System.String
The spelled-out number (e.g., "three hundred twenty-one", "forty-two"). Must not be null.
culture System.Globalization.CultureInfo
The culture to use for parsing. Different cultures may have different word representations for numbers (e.g., "twenty" in English vs. "vingt" in French).
Returns
System.Int32
The integer value represented by the words.
Exceptions
System.FormatException
Thrown when the input contains unrecognized words or cannot be parsed as a number.
System.ArgumentNullException
Thrown when words is null.
Example
// English (en-US)
"three hundred twenty-one".ToNumber(new CultureInfo("en-US")) => 321
"forty-two".ToNumber(new CultureInfo("en-US")) => 42
"one thousand".ToNumber(new CultureInfo("en-US")) => 1000
// Invalid input throws exception
"xyz".ToNumber(new CultureInfo("en-US")) => throws FormatException
Remarks
This method strictly parses the input and throws an exception if any word is not recognized. For a non-throwing version, use TryToNumber(this string, int, CultureInfo).
WordsToNumberExtension.TryToNumber(this string, int, CultureInfo) Method
Attempts to convert a spelled-out number string to its integer representation without throwing exceptions.
public static bool TryToNumber(this string words, out int parsedNumber, System.Globalization.CultureInfo culture);
Parameters
words System.String
The spelled-out number (e.g., "forty-two", "one hundred"). Must not be null.
parsedNumber System.Int32
When this method returns, contains the integer value represented by the words if the conversion succeeded, or 0 if the conversion failed.
culture System.Globalization.CultureInfo
The culture to use for parsing.
Returns
System.Boolean
true if the conversion was successful; otherwise, false.
Example
// Successful conversion
"forty-two".TryToNumber(out int result, new CultureInfo("en-US")) => returns true, result = 42
// Failed conversion
"xyz".TryToNumber(out int result, new CultureInfo("en-US")) => returns false, result = 0
Remarks
This is the recommended method for parsing when you're not sure if the input is valid. It does not throw exceptions on invalid input.
WordsToNumberExtension.TryToNumber(this string, int, CultureInfo, string) Method
Attempts to convert a spelled-out number string to its integer representation and provides the first unrecognized word if the conversion fails.
public static bool TryToNumber(this string words, out int parsedNumber, System.Globalization.CultureInfo culture, out string? unrecognizedWord);
Parameters
words System.String
The spelled-out number (e.g., "one thousand one"). Must not be null.
parsedNumber System.Int32
When this method returns, contains the integer value represented by the words if the conversion succeeded, or 0 if the conversion failed.
culture System.Globalization.CultureInfo
The culture to use for parsing.
unrecognizedWord System.String
When this method returns false, contains the first unrecognized word found in the input.
When this method returns true, this parameter is set to null.
Returns
System.Boolean
true if the conversion was successful; otherwise, false.
Example
// Successful conversion
"one thousand".TryToNumber(out int result, new CultureInfo("en-US"), out string? badWord)
=> returns true, result = 1000, badWord = null
// Failed conversion with unrecognized word
"one xyz three".TryToNumber(out int result, new CultureInfo("en-US"), out string? badWord)
=> returns false, result = 0, badWord = "xyz"
Remarks
This overload is useful for debugging or providing detailed error messages to users, as it identifies which specific word caused the parsing failure.