The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.text  [26 examples] > Numbers  [5 examples]

e315. Formatting and Parsing a Locale-Specific Percentage

    // Format
    Locale locale = Locale.CANADA;
    String string = NumberFormat.getPercentInstance(locale).format(123.45);
    // 12,345%
    
    // Parse
    try {
        Number number = NumberFormat.getPercentInstance(locale).parse("123.45%");
        // 1.2345
        if (number instanceof Long) {
            // Long value
        } else {
            // Double value
        }
    } catch (ParseException e) {
    }

 Related Examples
e311. Formatting a Number Using a Custom Format
e312. Formatting and Parsing a Number for a Locale
e313. Formatting a Number in Exponential Notation
e314. Formatting and Parsing Locale-Specific Currency

See also: Dates    Messsages    Times    Words and Sentences   


© 2002 Addison-Wesley.