Parse()

Namespace ›› System ›› MfInteger ›› Methods ››
Parent Previous Next

Parse()

OutputVar := MfInteger.Parse(s)
OutputVar := MfInteger.Parse(s, style)
OutputVar := MfInteger.Parse(s, provider)
OutputVar := MfInteger.Parse(s, style, provider)

MfInteger.Parse(s)

Converts the s representation of a number to its MfInteger equivalent

Parameters

s

An string var of object convert.
Can be var or instance of MfChar, MfString

Returns

Returns MfInteger instance equivalent to the number contained in s.

Throws

Throws MfArgumentNullException if s is null.
Throws MfFormatException if s is not of the correct format.
Throws MfOverFlowExcpetion s represents a number less than MinValue or greater than MaxValue.

Remarks

Static method
The s parameter contains a number of the form:

[ws][sign]digits[ws]

Elements in square brackets ([ and ]) are optional. The following table describes each element.

Element

Description

ws

Optional white space.

sign

An optional positive or negative sign.

digits

A sequence of digits ranging from 0 to 9

The s parameter is interpreted using the MfNumberStyles.Integer style. In addition to the byte value's decimal digits, only leading and trailing spaces together with a leading sign are allowed. (If the sign is present, it must be a positive sign or the method throws an MfOverFlowExcpetion.)

To explicitly define the style elements that can be present in s, use either the MfInteger.Parse(s, style) or the MfInteger.Parse(s, style, provider) method.

The s parameter is parsed using the formatting information in a MfNumberFormatInfo object. To parse a string using other formatting information, use the MfInteger.Parse(s, style, provider) method.

Example

sb := new MfText.StringBuilder()

value := "  179042  "
Convert32(value, sb)

value := " -2041326 "
Convert32(value, sb)

value := "   1064.0   "
Convert32(value, sb)

value := "  178.3"
Convert32(value, sb)

value := ""
Convert32(value, sb)

value := MfInteger.MaxValue . "1"
Convert32(value, sb)

MsgBox % sb.ToString()

Convert32(value, sb)
{
   try
   {
       number := MfInteger.Parse(value)
       sb.AppendFormat("Converted '{0}' to {1}.", value, number)
       sb.AppendLine()
   }
   catch e
   {
       if (MfObject.IsObjInstance(e, MFFormatException))
       {
           sb.AppendFormat("Unable to convert '{0}'.", value)
           sb.AppendLine()
       }
       else if (MfObject.IsObjInstance(e, MfOverflowException))
       {
           sb.AppendFormat("'{0}' is out of range int32", value)
           sb.AppendLine()
       }
   }
}

/*
Converted '  179042  ' to 179042.
Converted ' -2041326 ' to -2041326.
Unable to convert '   1064.0   '.
Unable to convert '  178.3'.
Unable to convert ''.
'21474836471' is out of range int32

*/

MfInteger.Parse(s, style)

Converts the s representation of a number to its MfInteger equivalent

Parameters

s

An string var of object convert.
Can be var or instance of MfChar, MfString

style

MfNumberStyles Instance or Integer var representing instance.
A bitwise combination of MfNumberStyles values that indicates the style elements that can be present in s. A typical value to specify is MfNumberStyles.Integer.

Returns

Returns MfInteger instance equivalent to the number contained in s.

Throws

Throws MfInvalidOperationException if not called as static method.
Throws MfArgumentNullException if s is null.
Throws MfFormatException if s is not of the correct format.
Throws MfOverFlowExcpetion s represents a number less than MinValue or greater than MaxValue.
Throws MfArgumentException style is not a MfNumberStyles value.
-or-
style is not a combination of MfNumberStyles.AllowHexSpecifier and MfNumberStyles.HexNumber values.

Since

Version 0.4

Remarks

Static method
The style parameter defines the style elements (such as white space or the positive sign) that are allowed in the s parameter for the parse operation to succeed. It must be a combination of bit flags from the MfNumberStyles enumeration. Depending on the value of style, the s parameter may include the following elements:

[ws][$][sign]digits[.fractional_digits][e[sign]digits][ws]

Elements in square brackets ([ and ]) are optional. The following table describes each element.

Element

Description

ws

Optional white space. White space can appear at the beginning of s if style includes the MfNumberStyles.AllowLeadingWhite flag, or at the end of s if style includes the MfNumberStyles.AllowTrailingWhite flag.

$

A currency symbol. Its position in the string is defined by the MfNumberFormatInfo.CurrencyPositivePattern property. The current culture's currency symbol can appear in s if style includes the MfNumberStyles.AllowCurrencySymbol flag.

sign

An optional positive or negative sign.

digits

A sequence of digits ranging from 0 to 9

.

A decimal point symbol. The default decimal point symbol can appear in s if style includes the MfNumberStyles.AllowDecimalPoint flag.

fractional_digits

One or more occurrences of the digit 0. Fractional digits can appear in s only if style includes the MfNumberStyles.AllowDecimalPoint flag.

e

The e or E character, which indicates that the value is represented in exponential notation. The s parameter can represent a number in exponential notation if style includes the MfNumberStyles.AllowExponent flag.

hexdigits

A sequence of hexadecimal digits from 0 through f, or 0 through F.

A string with decimal digits only (which corresponds to the MfNumberStyles.None style) always parses successfully. Most of the remaining MfNumberStyles enumerations control elements that may be but are not required to be present in this input string. The following table indicates how individual MfNumberStyles enumerations affect the elements that may be present in s.

Non-composite NumberStyles values

Elements permitted in s in addition to digits

MfNumberStyles.None

Decimal digits only.

MfNumberStyles.AllowDecimalPoint

The . and fractional_digits elements. However, fractional_digits must consist of only one or more 0 digits or an MfOverflowException is thrown.

MfNumberStyles.AllowExponent

The s parameter can also use exponential notation.

MfNumberStyles.AllowLeadingWhite

The ws element at the beginning of s.

MfNumberStyles.AllowTrailingWhite

The ws element at the end of s.

MfNumberStyles.AllowLeadingSign

A positive sign can appear before digits.

MfNumberStyles.AllowTrailingSign

A positive sign can appear after digits.

MfNumberStyles.AllowParentheses

Although this flag is supported, the use of parentheses in s results in an MfOverflowException.

MfNumberStyles.AllowThousands

Although the group separator symbol can appear in s, it can be preceded by only one or more 0 digits.

MfNumberStyles.AllowCurrencySymbol

The $ element.

If the MfNumberStyles.AllowHexSpecifier flag is used, s must be a hexadecimal value without a prefix. For example, "C9AF3" parses successfully, but "0xC9AF3" does not. The only other flags that can be combined with it are MfNumberStyles.AllowLeadingWhite and MfNumberStyles.AllowTrailingWhite. (The MfNumberStyles enumeration includes a composite number style, MfNumberStyles.HexNumber, that includes both white space flags.)

The s parameter is parsed using the formatting information in a MfNumberFormatInfo object that is initialized. To use the formatting information of some other, call the MfInteger.Parse(s, style, provider) overload.

Example

sb := new MfText.StringBuilder()

Convert32("104.0", MfNumberStyles.Instance.AllowDecimalPoint, sb)
Convert32("104.9", MfNumberStyles.Instance.AllowDecimalPoint, sb)
Convert32(" 106034", MfNumberStyles.Instance.None, sb)
Convert32(" $17,198,064.42", MfNumberStyles.Instance.AllowCurrencySymbol.Value
   | MfNumberStyles.Instance.Number.Value, sb)
Convert32(" $17,198,064.00", MfNumberStyles.Instance.AllowCurrencySymbol.Value
   | MfNumberStyles.Instance.Number.Value, sb)

Convert32("103E06", MfNumberStyles.Instance.AllowExponent, sb)
Convert32("1200E-02", MfNumberStyles.Instance.AllowExponent, sb)
Convert32("1200E-03", MfNumberStyles.Instance.AllowExponent, sb)
Convert32("-1,345,791", MfNumberStyles.Instance.AllowThousands, sb)
Convert32("(1,345,791)",  MfNumberStyles.Instance.AllowThousands.Value
   | MfNumberStyles.Instance.AllowParentheses.Value, sb)
Convert32("FFCA00A0", MfNumberStyles.Instance.HexNumber, sb)
Convert32("0xFFCA00A0", MfNumberStyles.Instance.HexNumber, sb)

MsgBox % sb.ToString()

Convert32(value, style, sb)
{
   try
   {
       number := MfInteger.Parse(value, style)
       sb.AppendFormat("Converted '{0}' to {1}.", value, number)
       sb.AppendLine()
   }
   catch e
   {
       if (MfObject.IsObjInstance(e, MFFormatException))
       {
           sb.AppendFormat("Unable to convert '{0}'.", value)
           sb.AppendLine()
       }
       else if (MfObject.IsObjInstance(e, MfOverflowException))
       {
           sb.AppendFormat("'{0}' is out of range Int32 type.", value)
           sb.AppendLine()
       }
   }
}

/*
Converted '104.0' to 104.
'104.9' is out of range Int32 type.
Unable to convert ' 106034'.
' $17,198,064.42' is out of range Int32 type.
Converted ' $17,198,064.00' to 17198064.
Converted '103E06' to 103000000.
Converted '1200E-02' to 12.
'1200E-03' is out of range Int32 type.
Unable to convert '-1,345,791'.
Converted '(1,345,791)' to -1345791.
Converted 'FFCA00A0' to -3538784.
Unable to convert '0xFFCA00A0'.

*/

MfInteger.Parse(s, provider)

Converts the s representation of a number to its MfInteger equivalent

Parameters

s

An string var of object convert.
Can be var or instance of MfChar, MfString

provider

An object that supplies culture-specific parsing information about s. If provider is null, then MfNumberFormatInfo is used.

Throws

Throws MfInvalidOperationException if not called as static method.
Throws MfArgumentNullException if s is null.
Throws MfFormatException if s is not of the correct format.
Throws MfOverFlowExcpetion s represents a number less than MinValue or greater than MaxValue.

Returns

Returns MfInteger instance equivalent to the number contained in s.

Since

Version 0.4

Remarks

Static method
The s parameter contains a number of the form:

[ws][sign]digits[ws]

Elements in square brackets ([ and ]) are optional. The following table describes each element.

Element

Description

ws

Optional white space.

sign

An optional positive or negative sign.

digits

A sequence of digits ranging from 0 to 9

The s parameter is interpreted using the MfNumberStyles.Integer style. In addition to the byte value's decimal digits, only leading and trailing spaces together with a leading sign are allowed. (If the sign is present, it must be a positive sign or the method throws an MfOverFlowExcpetion.)

To explicitly define the style elements that can be present in s, use either the MfInteger.Parse(s, style) or the MfInteger.Parse(s, style, provider) method.

The s parameter is parsed using the formatting information in a MfNumberFormatInfo object. To parse a string using other formatting information, use the MfInteger.Parse(s, style, provider) method.

MfInteger.Parse(s, style, provider)

Converts the s representation of a number to its MfInteger equivalent

Parameters

s

An string var of object convert.
Can be var or instance of MfChar, MfString

style

MfNumberStyles Instance or Integer var representing instance.
A bitwise combination of MfNumberStyles values that indicates the style elements that can be present in s. A typical value to specify is MfNumberStyles.Integer.

provider

An object that supplies culture-specific parsing information about s. If provider is null, then MfNumberFormatInfo is used.

Returns

Returns MfInteger instance equivalent to the number contained in s.

Since

Version 0.4

Throws

Throws MfInvalidOperationException if not called as static method.
Throws MfArgumentNullException if s is null.
Throws MfFormatException if s is not of the correct format.
Throws MfOverFlowExcpetion s represents a number less than MinValue or greater than MaxValue.
Throws MfArgumentException style is not a MfNumberStyles value.
-or-
style is not a combination of MfNumberStyles.AllowHexSpecifier and MfNumberStyles.HexNumber values.

Remarks

Static method
The style parameter defines the style elements (such as white space or the positive sign) that are allowed in the s parameter for the parse operation to succeed. It must be a combination of bit flags from the MfNumberStyles enumeration. Depending on the value of style, the s parameter may include the following elements:

[ws][$][sign]digits[.fractional_digits][e[sign]digits][ws]

Elements in square brackets ([ and ]) are optional. The following table describes each element.

Element

Description

ws

Optional white space. White space can appear at the beginning of s if style includes the MfNumberStyles.AllowLeadingWhite flag, or at the end of s if style includes the MfNumberStyles.AllowTrailingWhite flag.

$

A currency symbol. Its position in the string is defined by the MfNumberFormatInfo.CurrencyPositivePattern property. The current culture's currency symbol can appear in s if style includes the MfNumberStyles.AllowCurrencySymbol flag.

sign

An optional positive or negative sign.

digits

A sequence of digits ranging from 0 to 9

.

A decimal point symbol. The default decimal point symbol can appear in s if style includes the MfNumberStyles.AllowDecimalPoint flag.

fractional_digits

One or more occurrences of the digit 0. Fractional digits can appear in s only if style includes the MfNumberStyles.AllowDecimalPoint flag.

e

The e or E character, which indicates that the value is represented in exponential notation. The s parameter can represent a number in exponential notation if style includes the MfNumberStyles.AllowExponent flag.

hexdigits

A sequence of hexadecimal digits from 0 through f, or 0 through F.

A string with decimal digits only (which corresponds to the MfNumberStyles.None style) always parses successfully. Most of the remaining MfNumberStyles enumerations control elements that may be but are not required to be present in this input string. The following table indicates how individual MfNumberStyles enumerations affect the elements that may be present in s.

Non-composite NumberStyles values

Elements permitted in s in addition to digits

MfNumberStyles.None

Decimal digits only.

MfNumberStyles.AllowDecimalPoint

The . and fractional_digits elements. However, fractional_digits must consist of only one or more 0 digits or an MfOverflowException is thrown.

MfNumberStyles.AllowExponent

The s parameter can also use exponential notation.

MfNumberStyles.AllowLeadingWhite

The ws element at the beginning of s.

MfNumberStyles.AllowTrailingWhite

The ws element at the end of s.

MfNumberStyles.AllowLeadingSign

A positive sign can appear before digits.

MfNumberStyles.AllowTrailingSign

A positive sign can appear after digits.

MfNumberStyles.AllowParentheses

Although this flag is supported, the use of parentheses in s results in an MfOverflowException.

MfNumberStyles.AllowThousands

Although the group separator symbol can appear in s, it can be preceded by only one or more 0 digits.

MfNumberStyles.AllowCurrencySymbol

The $ element.

If the MfNumberStyles.AllowHexSpecifier flag is used, s must be a hexadecimal value without a prefix. For example, "C9AF3" parses successfully, but "0xC9AF3" does not. The only other flags that can be combined with it are MfNumberStyles.AllowLeadingWhite and MfNumberStyles.AllowTrailingWhite. (The MfNumberStyles enumeration includes a composite number style, MfNumberStyles.HexNumber, that includes both white space flags.)

Related

TryParse(), GetValue()