Home  |  Delphi .net Home  |  System.Single  |  Parse Method
Parse  
Method  
Converts a string representation of a Single into a Single value
Single Structure
System NameSpace
CF1.  Function Parse ( Value : String; ) : Single;
CF2.  Function Parse ( Value:StringValue : String; Style : NumberStyles; ) : Single;
CF3.  Function Parse ( Value:StringValue : String; FormatProvider : IFormatProvider; ) : Single;
CF4.  Function Parse ( Value:StringValue : String; Style : NumberStyles; FormatProvider : IFormatProvider; ) : Single; Static;
CF : Methods with this mark are Compact Framework Compatible
Description
Attempts to parse the Value string into a value in the range +/-3.402823e38, returning a Single object with this value.
 
The Style parameter determines the allowed number content. It is an enumerated type that is treated as a set of flags (it has the [Flags] attribute. This means that multiple values may be set, using logical or. The possible values are :
 
AllowCurrencySymbol Allow for ?,$ ...
AllowExponentE+000 format
AllowThousandsFor example : 1,000,000
AllowDecimalPointFor example : 123.456
AllowParenthesesFor example (1234)
AllowTrailingSignFor example : 123-
AllowLeadingSignFor example : -123
AllowTrailingWhiteAllow trailing blanks
AllowLeadingWhiteAllow leading blanks
AllowHexSpecifierFor example : 0x2bcd

 
The FormatProvider option allows for customised formatting and is beyond the scope of Delphi Basics.
Notes
Warning : An exception is thrown if the parse encounters unexpected characters.

Static methods are not methods of an object - they are simply class functions or procedures available at any time.
References
NumberStyles
Microsoft MSDN Links
System
System.Single
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

var
  dblStr : String;
  result : Single;

begin
  dblStr := '-123.45';
  result := System.Single.Parse(dblStr);
  Console.WriteLine('''' + dblStr + ''' parses to {0}', result.ToString);

  Console.ReadLine;
end.
Show full unit code
  '-123.45' parses to -123.45
Using NumberStyles
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Globalization;

var
  style  : NumberStyles;
  dblStr : String;
  result : Double;

begin
  // Allow for hex values and leading/trailing blanks
  style := NumberStyles.AllowLeadingWhite  or
           NumberStyles.AllowTrailingWhite or
           NumberStyles.AllowThousands;

  dblStr := ' 123,456,789 ';  
  result := System.Double.Parse(dblStr, style);
  Console.WriteLine('''' + dblStr + ''' parses to {0}', result.ToString);

  // Or more simply using one combined number style value
  style := NumberStyles.HexNumber;

  dblStr := ' AC ';   // Hex AC = 189
  result  := System.Double.Parse(dblStr, style);
  Console.WriteLine('''' + dblStr + '''          parses to {0}', result.ToString);

  Console.ReadLine;
end.
Show full unit code
  ' 123,456,789 ' parses to 123456789
  ' AC '          parses to 189
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author