Freigeben über


DecimalFormat.Parse(String, ParsePosition) Method

Definition

Parses text from a string to produce a Number.

[Android.Runtime.Register("parse", "(Ljava/lang/String;Ljava/text/ParsePosition;)Ljava/lang/Number;", "GetParse_Ljava_lang_String_Ljava_text_ParsePosition_Handler")]
public override Java.Lang.Number? Parse (string? text, Java.Text.ParsePosition? pos);
[<Android.Runtime.Register("parse", "(Ljava/lang/String;Ljava/text/ParsePosition;)Ljava/lang/Number;", "GetParse_Ljava_lang_String_Ljava_text_ParsePosition_Handler")>]
override this.Parse : string * Java.Text.ParsePosition -> Java.Lang.Number

Parameters

text
String

the string to be parsed

pos
ParsePosition

A ParsePosition object with index and error index information as described above.

Returns

the parsed value, or null if the parse fails

Attributes

Remarks

Parses text from a string to produce a Number.

The method attempts to parse text starting at the index given by pos. If parsing succeeds, then the index of pos is updated to the index after the last character used (parsing does not necessarily use all characters up to the end of the string), and the parsed number is returned. The updated pos can be used to indicate the starting point for the next call to this method. If an error occurs, then the index of pos is not changed, the error index of pos is set to the index of the character where the error occurred, and null is returned.

The subclass returned depends on the value of #isParseBigDecimal as well as on the string being parsed. <ul> <li>If isParseBigDecimal() is false (the default), most integer values are returned as Long objects, no matter how they are written: "17" and "17.000" both parse to Long(17). Values that cannot fit into a Long are returned as Doubles. This includes values with a fractional part, infinite values, NaN, and the value -0.0. DecimalFormat does <em>not</em> decide whether to return a Double or a Long based on the presence of a decimal separator in the source string. Doing so would prevent integers that overflow the mantissa of a double, such as "-9,223,372,036,854,775,808.00", from being parsed accurately.

Callers may use the Number methods doubleValue, longValue, etc., to obtain the type they want. <li>If isParseBigDecimal() is true, values are returned as BigDecimal objects. The values are the ones constructed by java.math.BigDecimal#BigDecimal(String) for corresponding strings in locale-independent format. The special cases negative and positive infinity and NaN are returned as Double instances holding the values of the corresponding Double constants. </ul>

DecimalFormat parses all Unicode characters that represent decimal digits, as defined by Character.digit(). In addition, DecimalFormat also recognizes as digits the ten consecutive characters starting with the localized zero digit defined in the DecimalFormatSymbols object.

Java documentation for java.text.DecimalFormat.parse(java.lang.String, java.text.ParsePosition).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to