Ignore:
Timestamp:
2011-10-24T21:27:10+02:00 (8 years ago)
Author:
Don-vip
Message:

Fixed rounding error in LatLon.roundToOsmPrecision()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/coor/LatLon.java

    r4476 r4541  
    1212import static java.lang.Math.toRadians;
    1313
     14import java.math.BigDecimal;
     15import java.math.MathContext;
    1416import java.text.DecimalFormat;
    1517import java.text.NumberFormat;
     
    2830public class LatLon extends Coordinate {
    2931
     32   
    3033    /**
    3134     * Minimum difference in location to not be represented as the same position.
     
    3336     */
    3437    public static final double MAX_SERVER_PRECISION = 1e-7;
     38    public static final int    MAX_SERVER_DIGITS = 7;
    3539
    3640    private static DecimalFormat cDmsMinuteFormatter = new DecimalFormat("00");
     
    240244     */
    241245    public static double roundToOsmPrecision(double value) {
    242         return Math.round(value / MAX_SERVER_PRECISION) * MAX_SERVER_PRECISION;
     246        double absV = Math.abs(value);
     247        int numOfDigits = MAX_SERVER_DIGITS + (absV < 1 ? 0 : (absV < 10 ? 1 : (absV < 100 ? 2 : 3)));
     248        return BigDecimal.valueOf(value).round(new MathContext(numOfDigits)).doubleValue();
     249        //return Math.round(value / MAX_SERVER_PRECISION) * MAX_SERVER_PRECISION; // Old method, causes rounding errors (see LatLonTest) !
    243250    }
    244251   
Note: See TracChangeset for help on using the changeset viewer.