I have noticed with testing GPS locations, a value such as −123 might come up on Android as −23.83333336. Is there any way to format incoming GPS locations, such as w开发者_如何学Cithin two decimal points?
This way I can store values like −123.83 opposed to longer numeric values. I have checked out DecimalFormat
and NumberFormat
, but these are strings and my numbers are doubles, and 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.
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
You could multiply it by one hundred and store it in an integer if you're trying to replicate a fixed-precision number.
精彩评论