Parse a number using a NumberFormat : Convert from String « Data Type « Java






Parse a number using a NumberFormat

  
import java.text.NumberFormat;
import java.text.ParseException;

/*
 * Parse a number using a NumberFormat.
 */
public class NumFormatParse {
  //+
  /** A number to parse */
  public static final String input = "4096.251";
  //-

  /** The main (and only) method in this class. */
  public static void main(String[] av) { 

    //+
    NumberFormat defForm = NumberFormat.getInstance();

    try {
      Number d = defForm.parse(input);
      System.out.println(input + 
        " parses as " + d +
        " and formats as " + defForm.format(d));
    } catch (ParseException pe) {
      System.err.println(input + "not parseable!");
    }
    //-
  }
}



           
         
    
  








Related examples in the same category

1.Convert from String to integer
2.Pass a string to the Integer class constructor and call the intValue()
3.Parse and format a number to octal
4.Parse and format a number to decimal
5.Parse and format to hexadecimal
6.Parse and format to arbitrary radix <= Character.MAX_RADIX
7.Decode string to integer
8.Convert string to an integer or number
9.Validate if string is a number
10.Declaring Checked Exceptions
11.Converting a String to a byte Number
12.Converting a String to a short Number
13.Converting a String to a int(integer) Number
14.Value Of Demo