Share via


StringExtensions.AsDecimal Method (String, Decimal)

Converts a string to a Decimal number and specifies a default value.

Namespace:  System.Web.WebPages
Assembly:  System.Web.WebPages (in System.Web.WebPages.dll)

Syntax

'Declaration
<ExtensionAttribute> _
Public Shared Function AsDecimal ( _
    value As String, _
    defaultValue As Decimal _
) As Decimal
'Usage
Dim value As String 
Dim defaultValue As Decimal 
Dim returnValue As Decimal 

returnValue = value.AsDecimal(defaultValue)
public static decimal AsDecimal(
    this string value,
    decimal defaultValue
)
[ExtensionAttribute]
public:
static Decimal AsDecimal(
    String^ value, 
    Decimal defaultValue
)
static member AsDecimal : 
        value:string * 
        defaultValue:decimal -> decimal
public static function AsDecimal(
    value : String, 
    defaultValue : decimal
) : decimal

Parameters

  • defaultValue
    Type: System.Decimal
    The value to return if value is null reference (Nothing in Visual Basic) or invalid.

Return Value

Type: System.Decimal
The converted value.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type String. When you use instance method syntax to call this method, omit the first parameter. For more information, see https://msdn.microsoft.com/en-us/library/bb384936(v=vs.111) or https://msdn.microsoft.com/en-us/library/bb383977(v=vs.111).

Remarks

For more information about how to use this method, see the AsDecimal() topic. If you think that your code might be passed null reference (Nothing in Visual Basic), an empty string (""), a value outside of the numeric range of the specified type, or a string value that cannot be converted to the specified type, you can use the AsDecimal overload to return a specified Decimal value instead of 0.

See Also

Reference

StringExtensions Class

AsDecimal Overload

System.Web.WebPages Namespace