Ignore:
Timestamp:
2020-07-15T18:58:09+02:00 (4 years ago)
Author:
stoecker
Message:

fix #19508 - better reversal function for number, patch by gaben

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/corrector/ReverseWayTagCorrector.java

    r16438 r16771  
    2828import org.openstreetmap.josm.data.osm.Tagged;
    2929import org.openstreetmap.josm.data.osm.Way;
     30import org.openstreetmap.josm.tools.Logging;
    3031import org.openstreetmap.josm.tools.UserCancelException;
    3132
     
    202203
    203204    /**
    204      * Inverts sign of a numeric value.
     205     * Inverts sign of a numeric value and converts decimal number to use decimal point.
     206     * Also removes sign from null value.
    205207     * @param value numeric value
    206208     * @return opposite numeric value
    207209     */
    208210    public static String invertNumber(String value) {
    209         Pattern pattern = Pattern.compile("^([+-]?)(\\d.*)$", Pattern.CASE_INSENSITIVE);
     211        Pattern pattern = Pattern.compile("^([+-]?)(\\d*[,.]?\\d*)(.*)$", Pattern.CASE_INSENSITIVE);
    210212        Matcher matcher = pattern.matcher(value);
    211213        if (!matcher.matches()) return value;
    212214        String sign = matcher.group(1);
    213         String rest = matcher.group(2);
     215        String number = matcher.group(2);
     216        String symbol = matcher.group(3);
    214217        sign = "-".equals(sign) ? "" : "-";
    215         return sign + rest;
     218
     219        if (!number.isEmpty()) {
     220            String fixedNum = number.replace(",", ".");
     221            try {
     222                double parsed = Double.parseDouble(fixedNum);
     223                if (parsed != 0) {
     224                    return sign + fixedNum + symbol;
     225                } else {
     226                    return fixedNum + symbol;
     227                }
     228            } catch (NumberFormatException e) {
     229                Logging.trace(e);
     230                return value;
     231            }
     232        }
     233
     234        return value;
    216235    }
    217236
Note: See TracChangeset for help on using the changeset viewer.