001package jmri.util;
002
003/**
004 * Common utility methods for Internationalization (I18N) and Localization
005 * (L12N) in the default {@link java.util.Locale}.
006 *
007 * See http://jmri.org/help/en/html/doc/Technical/I8N.shtml
008 *
009 * @author Bob Jacobsen Copyright 2014
010 * @since 3.9.3
011 */
012public class IntlUtilities {
013
014    /**
015     * Parse a number.
016     *
017     * Use as a locale-aware replacement for Float.valueOf("1").floatValue() and
018     * Float.parseFloat("1").floatValue().
019     *
020     * @param val the value to parse
021     * @return a parsed number
022     * @throws java.text.ParseException if val cannot be parsed as a number
023     */
024    static public float floatValue(String val) throws java.text.ParseException {
025        return java.text.NumberFormat.getInstance().parse(val).floatValue();
026    }
027
028    /**
029     * Parse a number.
030     *
031     * Use as a locale-aware replacement for Double.valueOf("1").doubleValue()
032     * and Double.parseDouble("1").doubleValue().
033     *
034     * @param val the value to parse
035     * @return a parsed number
036     * @throws java.text.ParseException if val cannot be parsed as a number
037     */
038    static public double doubleValue(String val) throws java.text.ParseException {
039        return java.text.NumberFormat.getInstance().parse(val).doubleValue();
040    }
041
042    /**
043     * Get the text representation of a number.
044     *
045     * Use as a locale-aware replacement for String.valueOf(2.3).
046     *
047     * @param val the number
048     * @return the text representation
049     */
050    static public String valueOf(double val) {
051        return java.text.NumberFormat.getInstance().format(val);
052    }
053
054    /**
055     * Get the text representation of a number.
056     *
057     * Use as a locale-aware replacement for String.valueOf(5).
058     *
059     * @param val the number
060     * @return the text representation
061     */
062    static public String valueOf(int val) {
063        return java.text.NumberFormat.getInstance().format(val);
064    }
065
066    // initialize logging
067    //private final static Logger log = LoggerFactory.getLogger(IntlUtilities.class);
068}