java.util.DecimalFormat.parse() - Parsing Strings to Number Objects

This section provides a tutorial example on how to use the java.util.DecimalFormat.parse() method to parse or convert numeric strings to number objects.

DecimalFormat objects can also be used to parse strings for numbers by using the parse() method. Here is a demonstration program:

/* NumberParsingTest.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */
import java.util.*;
import java.text.*;
class NumberParsingTest {
   public static void main(String[] a) {
      parseNumbers();
   }
   public static void parseNumbers() {
      DecimalFormat nf = new DecimalFormat("#,##0.00");
      String ns = "-1234.5678";
      Number nv = null;
      DecimalFormat cf = new DecimalFormat("\u00A4#,##0.00");
      String cs = "-$1234.5678";
      Number cv = null;
      DecimalFormat xf 
         = new DecimalFormat("\u00A4#,##0.00;(\u00A4#,##0.00)");
      String xs = "($1234.5678)";
      Number xv = null;
      DecimalFormat pf = new DecimalFormat("#,##0.00%");
      String ps = "-1234.5678%";
      Number pv = null;
      try {
         nv = nf.parse(ns);
         cv = cf.parse(cs);
         xv = xf.parse(xs);
         pv = pf.parse(ps);
      } catch (ParseException e) {
         System.out.println("Exception: " + e.toString());
      }
      System.out.println("Parsing " + ns + " with " + nf.toPattern());
      System.out.println("   " + nv.doubleValue());                    
      System.out.println("Parsing " + cs + " with " + cf.toPattern());
      System.out.println("   " + cv.doubleValue());                    
      System.out.println("Parsing " + xs + " with " + xf.toPattern());
      System.out.println("   " + xv.doubleValue());                    
      System.out.println("Parsing " + ps + " with " + pf.toPattern());
      System.out.println("   " + pv.doubleValue());                    
   }
}

Output:

Parsing -51,234.5678 with #,##0.00
   -51234.5678
Parsing -$51234.5678 with ñ#,##0.00
   -51234.5678
Parsing ($51234.5678) with ñ#,##0.00;(ñ#,##0.00)
   -51234.5678
Parsing -51234.5678% with #,##0.00%
   -512.345678

The result is very interesting:

Last update: 2014.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 Java Date-Time API

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

Number Object and Numeric String Conversion

 java.util.NumberFormat - Formatting Numeric Values to Strings

java.util.DecimalFormat.parse() - Parsing Strings to Number Objects

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Secret Key Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials

 References

 PDF Printing Version