'How can i round 2 numbers but keep their rankings intact in node?

I have a distance calculation code where i take the distance and use toFixed(1) to make it a single digit after comma. The code looks like this:

const distance = parseFloat(!sortInfo?.length || sortInfo[0] === 'Infinity' ? null : sortInfo[0]).toFixed(1);

Now i looked for ways to rounding this distance value with things like math.round or epsilon but i wanted to know, for example the first distance we have is A which is 3.234km away, and B which is 3.245km away. I need to round both of these to 3.2km but still make the first location(A) the closest one. How can i achieve this?



Solution 1:[1]

If you really need to tell which distance is bigger you are forced to store the distance. The only alternative I see is to first sort by distance and then round the numbers, but then if a new distance comes in you won't be able to compare it to the ones you have rounded unless you stored the exact value somewhere else.

Sorting by distance from closest to farthest you can tell which one is closer than another by checking it's location in the array.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adrian