views:

279

answers:

2

I have noticed with testing GPS locations, a value such as -123 might come up on Android as -123.83333336... is there anyway to format incoming GPS locations, such as within 2 decimal points? This way I can store values like -123.83 opposed to longer numeric values. I have checked out DecimalFormat and NumberFormat, these are strings and my numbers are doubles, I cannot find a better way. And does anyone know a maximum value of a location in numeric values? Knowing that, I can do something like

DecimalFormat df = new DecimalFormat("####.##");

knowing the highest number is in the thousands, etc.

Thanks!

A: 

A function like this will round it without converting to a string:

public static final double roundDouble(double d, int places) {
    return Math.round(d * Math.pow(10, (double) places)) / Math.pow(10,
        (double) places);
}

For instance roundDouble(502.23525, 2) will produce 502.24

Jack Lloyd
A: 

You could multiply it by one hundred and store it in an integer if you're trying to replicate a fixed-precision number.

Andrew Lewis