NumberFormat parse() method in Java with Examples Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The parse(str) method is a built-in method of the java.text.NumberFormat which parses text from the beginning of the given string to produce a number. The method may not use the entire text of the given string Syntax: public Number parse?(String str) Parameters: The function accepts a string str whose beginning should be parsed. Return Value: The function returns a number parsed from the string. Exceptions: The function throws a ParseException if the beginning of the specified string cannot be parsed. Below is the implementation of the above function: Program 1: Java // Java program to implement // the above function import java.text.NumberFormat; import java.util.Locale; import java.text.ParsePosition; public class Main { public static void main(String[] args) throws Exception { // Get the number instance NumberFormat nF = NumberFormat.getNumberInstance(); // Prints the parsed number or NULL System.out.println("Number parsed: " + nF.parse("567")); } } Output: Number parsed: 567 The parse(str, parseIndex) method is a built-in method of the java.text.NumberFormat which parses a number from the text and returns a Long if possible, otherwise a Double. If IntegerOnly is set, will stop at a decimal point (or equivalent; e.g., for rational numbers "1 2/3", will stop after the 1). Syntax: public abstract Number parse(String str, ParsePosition parseIndex) Parameters: The function accepts two parameters which are described below: str: specifies the string to be parsed. parseIndex: specifies the parse position Return Value: The function returns a number parsed from the string. Below is the implementation of the above function: Program 1: Java // Java program to implement // the above function import java.text.NumberFormat; import java.util.Locale; import java.text.ParsePosition; public class Main { public static void main(String[] args) throws Exception { // Get the number instance NumberFormat nF = NumberFormat.getNumberInstance(); // Prints the parsed number or NULL System.out.println("Number parsed: " + nF.parse("567", new ParsePosition(1))); } } Output: Number parsed: 67 Reference: https://docs.oracle.com/javase/10/docs/api/java/text/NumberFormat.html#parse(java.lang.String) Comment More infoAdvertise with us Next Article NumberFormat setParseIntegerOnly() method in Java with Examples G gopaldave Follow Improve Article Tags : Java Java-Functions Java-text package Java-NumberFormat Practice Tags : Java Similar Reads NumberFormat parseObject() method in Java with Examples The parseObject() method is a built-in method of the java.text.NumberFormat which parses a text from a string to produce a Number. The function attempts to parse text starting at a given index. When parsing occurs, the given index is set to the last character used, in case parsing fails, the given i 2 min read NumberFormat parseObject() method in Java with Examples The parseObject() method is a built-in method of the java.text.NumberFormat which parses a text from a string to produce a Number. The function attempts to parse text starting at a given index. When parsing occurs, the given index is set to the last character used, in case parsing fails, the given i 2 min read NumberFormat setParseIntegerOnly() method in Java with Examples The setParseIntegerOnly() method is a built-in method of the java.text.NumberFormat which sets whether or not numbers should be parsed as integers only. Syntax: public void setParseIntegerOnly(boolean val) Parameters: The function accepts a mandatory parameter val which specifies the value to be set 1 min read NumberFormat setParseIntegerOnly() method in Java with Examples The setParseIntegerOnly() method is a built-in method of the java.text.NumberFormat which sets whether or not numbers should be parsed as integers only. Syntax: public void setParseIntegerOnly(boolean val) Parameters: The function accepts a mandatory parameter val which specifies the value to be set 1 min read NumberFormat hashCode() method in Java with Examples The hashCode() method is a built-in method of the java.text.NumberFormat returns a hash-code value for this given object of instance. Syntax: public int hashCode() Parameters: The function does not accepts any parameter. Return Value: The function returns the hash-code value. Below is the implementa 1 min read NumberFormat hashCode() method in Java with Examples The hashCode() method is a built-in method of the java.text.NumberFormat returns a hash-code value for this given object of instance. Syntax: public int hashCode() Parameters: The function does not accepts any parameter. Return Value: The function returns the hash-code value. Below is the implementa 1 min read Like