Class NumbersExtensions
- Namespace
- NuvTools.Common.Numbers
- Assembly
- NuvTools.Common.dll
Provides extension methods for parsing strings to numeric types with null-safe handling.
public static class NumbersExtensions
- Inheritance
-
NumbersExtensions
- Inherited Members
Remarks
The overloads without an IFormatProvider read the text in the current culture.
That decides the decimal separator: on a pt-BR machine "3.14" is read as 314. Text that
does not come from the user — a file, a database column, an API — is written in a fixed format,
so parse it with the overload that takes a provider, usually InvariantCulture.
Methods
ParseToDecimalOrNull(string?, bool)
Parses the string to decimal or null, in the current culture — see the class remarks before using it on text that did not come from the user.
public static decimal? ParseToDecimalOrNull(this string? value, bool returnZeroIsNull = false)
Parameters
valuestringValue to parsed.
returnZeroIsNullboolReturns zero instead of null when the value is empty or not a number.
Returns
- decimal?
Returns decimal or null.
ParseToDecimalOrNull(string?, IFormatProvider?, bool)
Parses the string to decimal or null, in the format provider describes.
public static decimal? ParseToDecimalOrNull(this string? value, IFormatProvider? provider, bool returnZeroIsNull = false)
Parameters
valuestringValue to parsed.
providerIFormatProviderThe culture the text is written in, for example InvariantCulture.
returnZeroIsNullboolReturns zero instead of null when the value is empty or not a number.
Returns
- decimal?
Returns decimal or null.
ParseToDoubleOrNull(string?, bool)
Parses the string to double or null, in the current culture — see the class remarks before using it on text that did not come from the user.
public static double? ParseToDoubleOrNull(this string? value, bool returnZeroIsNull = false)
Parameters
valuestringValue to parsed.
returnZeroIsNullboolReturns zero instead of null when the value is empty or not a number.
Returns
- double?
Returns double or null.
ParseToDoubleOrNull(string?, IFormatProvider?, bool)
Parses the string to double or null, in the format provider describes.
public static double? ParseToDoubleOrNull(this string? value, IFormatProvider? provider, bool returnZeroIsNull = false)
Parameters
valuestringValue to parsed.
providerIFormatProviderThe culture the text is written in, for example InvariantCulture.
returnZeroIsNullboolReturns zero instead of null when the value is empty or not a number.
Returns
- double?
Returns double or null.
ParseToIntOrNull(string?, bool)
Parses the string to int or null, in the current culture.
public static int? ParseToIntOrNull(this string? value, bool returnZeroIsNull = false)
Parameters
valuestringValue to parsed.
returnZeroIsNullboolReturns zero instead of null when the value is empty or not a number.
Returns
- int?
Returns int or null.
ParseToIntOrNull(string?, IFormatProvider?, bool)
Parses the string to int or null, in the format provider describes.
public static int? ParseToIntOrNull(this string? value, IFormatProvider? provider, bool returnZeroIsNull = false)
Parameters
valuestringValue to parsed.
providerIFormatProviderThe culture the text is written in, for example InvariantCulture.
returnZeroIsNullboolReturns zero instead of null when the value is empty or not a number.
Returns
- int?
Returns int or null.
ParseToLongOrNull(string?, bool)
Parses the string to long or null, in the current culture.
public static long? ParseToLongOrNull(this string? value, bool returnZeroIsNull = false)
Parameters
valuestringValue to parsed.
returnZeroIsNullboolReturns zero instead of null when the value is empty or not a number.
Returns
- long?
Returns long or null.
ParseToLongOrNull(string?, IFormatProvider?, bool)
Parses the string to long or null, in the format provider describes.
public static long? ParseToLongOrNull(this string? value, IFormatProvider? provider, bool returnZeroIsNull = false)
Parameters
valuestringValue to parsed.
providerIFormatProviderThe culture the text is written in, for example InvariantCulture.
returnZeroIsNullboolReturns zero instead of null when the value is empty or not a number.
Returns
- long?
Returns long or null.
ParseToShortOrNull(string?, bool)
Parses the string to short or null, in the current culture.
public static short? ParseToShortOrNull(this string? value, bool returnZeroIsNull = false)
Parameters
valuestringValue to parsed.
returnZeroIsNullboolReturns zero instead of null when the value is empty or not a number.
Returns
- short?
Returns short or null.
ParseToShortOrNull(string?, IFormatProvider?, bool)
Parses the string to short or null, in the format provider describes.
public static short? ParseToShortOrNull(this string? value, IFormatProvider? provider, bool returnZeroIsNull = false)
Parameters
valuestringValue to parsed.
providerIFormatProviderThe culture the text is written in, for example InvariantCulture.
returnZeroIsNullboolReturns zero instead of null when the value is empty or not a number.
Returns
- short?
Returns short or null.