- 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 program to implement// the above functionimportjava.text.NumberFormat;importjava.util.Locale;importjava.text.ParsePosition;publicclassMain {publicstaticvoidmain(String[] args)throwsException{// Get the number instanceNumberFormat nF= NumberFormat.getNumberInstance();// Prints the parsed number or NULLSystem.out.println("Number parsed: "+ nF.parse("567"));}}chevron_rightfilter_noneOutput: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 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))); } } |
Number parsed: 67
Reference: https://docs.oracle.com/javase/10/docs/api/java/text/NumberFormat.html#parse(java.lang.String)
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- NumberFormat clone() method in Java with Examples
- NumberFormat hashCode() method in Java with Examples
- NumberFormat equals() method in Java with Examples
- NumberFormat getAvailableLocales() method in Java with Examples
- NumberFormat getCurrency() method in Java with Examples
- NumberFormat getCurrencyInstance() method in Java with Examples
- NumberFormat getIntegerInstance() method in Java with Examples
- NumberFormat getInstance() method in Java with Examples
- NumberFormat getNumberInstance() method in Java with Examples
- NumberFormat getMaximumFractionDigits() method in Java with Examples
- NumberFormat getPercentInstance() method in Java with Examples
- NumberFormat getMinimumFractionDigits() method in Java with Examples
- NumberFormat getMaximumIntegerDigits() method in Java with Examples
- NumberFormat getRoundingMode() method in Java with Examples
- NumberFormat isGroupingUsed() method in Java with Examples
- NumberFormat getMinimumIntegerDigits() method in Java with Examples
- NumberFormat setMaximumFractionDigits() method in Java with Examples
- NumberFormat setMinimumIntegerDigits() method in Java with Examples
- NumberFormat parseObject() method in Java with Examples
- NumberFormat setRoundingMode() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

