Code example for DecimalFormat

Methods: format, parse

0
     * 
     * @param coordinate unformatted coordinate 
     * @return formatted coordinate or origin coordinate if parse exception occurs 
     */ 
    public static Number formatCoordinate(double coordinate){
        DecimalFormat df = new DecimalFormat("##.####");
        return formatCoordinate(coordinate,df);
    } 
 
    /** 
     * Formats given coordinate with given format 
     * @param coordinate unformatted coordinate 
     * @param df format 
     * @return formatted coordinate or origin coordinate if parse exception occurs 
     */ 
    public static Number formatCoordinate(double coordinate, DecimalFormat df){
        String format = df.format(coordinate);
        try{ 
            return df.parse(format);
        }catch (ParseException pe){
            return coordinate;
        } 
    }