C# Decimal Parse(String, NumberStyles)

Description

Decimal Parse(String, NumberStyles) converts the string representation of a number in a specified style to its Decimal equivalent.

Syntax

Decimal.Parse(String, NumberStyles) has the following syntax.


public static decimal Parse(
  string s,
  NumberStyles style
)

Parameters

Decimal.Parse(String, NumberStyles) has the following parameters.

  • s - The string representation of the number to convert.
  • style - A bitwise combination of NumberStyles values that indicates the style elements that can be present in s. A typical value to specify is Number.

Returns

Decimal.Parse(String, NumberStyles) method returns The Decimal number equivalent to the number contained in s as specified by style.

Example

The following code example uses the Parse(String, NumberStyles) method to parse the string representations of Decimal values using the en-US culture.


using System;/* ww w .j ava 2  s. co m*/
using System.Globalization;
public class MainClass{
  public static void Main(String[] argv){  
    string value;
    decimal number;
    NumberStyles style;
    
    // Parse string with a floating point value using NumberStyles.None.  
    value = "1234.12";
    style = NumberStyles.None;
    
    number = Decimal.Parse(value, style);  
    System.Console.WriteLine("'{0}' converted to {1}.", value, number);
    
    // Parse string with a floating point value and allow decimal point. 
    style = NumberStyles.AllowDecimalPoint;
    number = Decimal.Parse(value, style);  
    System.Console.WriteLine("'{0}' converted to {1}.", value, number);
    
    
    // Parse string with negative value in parentheses 
    value = "(1,234.34)";
    style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | 
            NumberStyles.AllowParentheses; 
    number = Decimal.Parse(value, style);  
    System.Console.WriteLine("'{0}' converted to {1}.", value, number);
    
    
    // Parse string using Number style 
    value = " -12,234.49 ";
    style = NumberStyles.Number;
    number = Decimal.Parse(value, style);  
    System.Console.WriteLine("'{0}' converted to {1}.", value, number);
    
  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version