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:

 * Copyright (c) All Rights Reserved.
import java.util.*;
import java.text.*;
class NumberParsingTest {
   public static void main(String[] a) {
   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());


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

The result is very interesting:

Table of Contents

 About This JDK Tutorial Book

 JDK (Java Development Kit)

 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

 Java Logging

 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 - Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials


 Full Version in PDF/EPUB