How do I parse a number for a locale?

Category: java.text, viewed: 3947 time(s).
package org.kodejava.example.text;

import java.util.Locale;
import java.text.NumberFormat;
import java.text.ParseException;

public class LocaleNumberParse {

    public static void main(String[] args) {
        try {
            //
            // In this example we are trying to parse a number string in a
            // defined format. Basically we want to covert the string for a
            // locale into a correct number value.
            //
            Number number =
                NumberFormat.getNumberInstance(Locale.JAPAN).parse("25,000.75");

            //
            // Just do some stuff with the number from the parse process
            //
            if (number instanceof Long) {
                System.out.println("This number is instanceof Long and the " +
                    "value is: " + number.longValue());
            } else if (number instanceof Double) {
                System.out.println("This number is instanceof Double and the " +
                    "value is: " + number.doubleValue());
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats