Strange casting and scaling of distance values in DistanceHandler.handlePosition()

Mateusz Jończyk7 years ago

Hello,
I have been reading the class DistanceHandler (how coordinates.filter works).
The code is here: https://github.com/tananaev/traccar/blob/9d4e735f180576098900b373fff06bc661309592/src/org/traccar/DistanceHandler.java#L60
I now understand most of it, but am puzzled by the following line:

    distance = BigDecimal.valueOf(distance).setScale(2, RoundingMode.HALF_EVEN).doubleValue();

This seems to convert the value of distance from double to BigDecimal, cut fractional decimal digits to two places (?) and then convert it back to double. The point of this all eludes me.
Wouldn't it be better if setScale(5, RoundingMode.HALF_EVEN) were used?

Anton Tananaev7 years ago

It is used for rounding, as far as I know. Distance is in meters, so 2 decimal points is more than enough to represent GPS accuracy. I think mostly it's done to avoid long decimal values in JSON. If you have any better solution feel free to send a pull request on GitHub.

Mateusz Jończyk7 years ago

From what I have searched in Google, this is the preferred method of rounding Java values to decimal places.

I think that it would be better to round the values when printing the JSON.

In the database the values are stored as double, they are output in JSON only when displayed on the screen, right?

Anton Tananaev7 years ago

They are stored in database in JSON.