Skip to main content
Version: 4.0 (next)

Humanizer.WordsToNumberExtension

WordsToNumberExtension Class

Converts localized number words back into numeric values. Parsing is culture-aware, honors locale inheritance, and supports the same natural high-range forms that the locale authoring data exposes through number.words and number.parse.

public static class WordsToNumberExtension

Inheritance System.Object → WordsToNumberExtension

Methods

WordsToNumberExtension.ToNumber(this string, CultureInfo) Method

Converts a spelled-out number string to its numeric representation.

public static long ToNumber(this string words, System.Globalization.CultureInfo culture);

Parameters

words System.String

The spelled-out number. Must not be null.

culture System.Globalization.CultureInfo

The culture to use for parsing.

Returns

System.Int64
The numeric value represented by words.

Exceptions

System.FormatException
If the input contains unrecognized words or cannot be parsed as a number.

System.ArgumentNullException
If words is null.

Remarks

This method now returns System.Int64 to support locale-authored high-range number parsing beyond System.Int32.MaxValue. Existing code that depended on an System.Int32 result should either switch the receiving type to System.Int64 or use an explicit checked conversion. Use TryToNumber(this string, long, CultureInfo) when you want a non-throwing parse path and the first unrecognized token.

WordsToNumberExtension.TryToNumber(this string, long, CultureInfo) Method

Attempts to convert a spelled-out number string to its numeric representation without throwing.

public static bool TryToNumber(this string words, out long parsedNumber, System.Globalization.CultureInfo culture);

Parameters

words System.String

The spelled-out number. Must not be null.

parsedNumber System.Int64

When this method returns, contains the parsed numeric value if successful; otherwise, 0.

culture System.Globalization.CultureInfo

The culture to use for parsing.

Returns

System.Boolean
true if the conversion was successful; otherwise, false.

Remarks

This is the recommended method when the input may be invalid.

WordsToNumberExtension.TryToNumber(this string, long, CultureInfo, string) Method

Attempts to convert a spelled-out number string to its numeric representation and reports the first unrecognized word if the conversion fails.

public static bool TryToNumber(this string words, out long parsedNumber, System.Globalization.CultureInfo culture, out string? unrecognizedWord);

Parameters

words System.String

The spelled-out number. Must not be null.

parsedNumber System.Int64

When this method returns, contains the parsed numeric value if successful; otherwise, 0.

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.

Remarks

This overload is useful for debugging or user-facing validation because it identifies the first token that could not be recognized.