From bfb194c5294c583dbf0bc0bef78b67d64a3a4f4c Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 5 Apr 2018 15:37:42 +0200 Subject: RouteWayCostUnitConverter: Snap values between 0–1 to 1 We use integer values for distance (kilometres) and time (minutes). If the values aren't very big, like bus stops inside a city for example, the integer conversion will put distance and time at `0`, which isn't correct. To continue to use our chosen measurement units while still displaying something that makes sense to users, snap any values >0 and <=1 to `1`. Refs #6404 --- lib/route_way_cost_unit_converter.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/route_way_cost_unit_converter.rb b/lib/route_way_cost_unit_converter.rb index 45edbf538..52515e52c 100644 --- a/lib/route_way_cost_unit_converter.rb +++ b/lib/route_way_cost_unit_converter.rb @@ -8,18 +8,24 @@ class RouteWayCostUnitConverter end end - private - # Round to 2 decimal places to appease JavaScript validation def self.meters_to_kilometers(num) return 0 unless num - (num / 1000.0).to_i + snap_to_one(num / 1000.0).to_i end def self.seconds_to_minutes(num) return 0 unless num - num / 60 + snap_to_one(num / 60.0).to_i + end + + private + + def self.snap_to_one(decimal) + return 1 if decimal > 0 && decimal <= 1 + + decimal end end -- cgit v1.2.3