Description
The ToDecimal
method is a static extension method provided by the SandboxSystemExtensions
class. It is used to convert a string representation of a number into a System.Decimal
value. If the conversion fails, it returns a specified default value.
Usage
To use the ToDecimal
method, call it on a string instance that you want to convert to a decimal. Provide a default decimal value that will be returned if the conversion is unsuccessful.
This method is particularly useful when you want to safely parse a string to a decimal without throwing exceptions for invalid input, and instead, use a fallback value.
Example
string numberString = "123.45";
System.Decimal defaultValue = 0.0m;
System.Decimal result = numberString.ToDecimal(defaultValue);
// result will be 123.45
string invalidNumberString = "abc";
result = invalidNumberString.ToDecimal(defaultValue);
// result will be 0.0, as the conversion fails and the default value is returned