diff options
| author | Teddy Wing | 2018-03-15 17:40:17 +0100 |
|---|---|---|
| committer | Luc Donnet | 2018-03-23 17:16:44 +0100 |
| commit | 8ff9c72521237f6f3ae5f2d7fc249da34137df6d (patch) | |
| tree | 10a2189a373e2993c2191fec6698d0752a5c96c3 | |
| parent | c113edee0c5575d4078ccb8f0d349992860535d7 (diff) | |
| download | chouette-core-8ff9c72521237f6f3ae5f2d7fc249da34137df6d.tar.bz2 | |
RouteWayCostUnitConverter: Round kilometres to 2 decimal places
The JavaScript validation doesn't pass if we have more than two decimal
places (I think because it uses a number field step value of 0.01).
Round the values to allow them to pass frontend validation.
Refs #6203
| -rw-r--r-- | lib/route_way_cost_unit_converter.rb | 3 | ||||
| -rw-r--r-- | spec/lib/route_way_cost_unit_converter_spec.rb | 7 |
2 files changed, 6 insertions, 4 deletions
diff --git a/lib/route_way_cost_unit_converter.rb b/lib/route_way_cost_unit_converter.rb index 1aebbbb14..979e92b0c 100644 --- a/lib/route_way_cost_unit_converter.rb +++ b/lib/route_way_cost_unit_converter.rb @@ -10,8 +10,9 @@ class RouteWayCostUnitConverter private + # Round to 2 decimal places to appease JavaScript validation def self.meters_to_kilometers(num) - num / 1000.0 + (num / 1000.0).round(2) end def self.seconds_to_minutes(num) diff --git a/spec/lib/route_way_cost_unit_converter_spec.rb b/spec/lib/route_way_cost_unit_converter_spec.rb index 33fb1d9aa..624ef5052 100644 --- a/spec/lib/route_way_cost_unit_converter_spec.rb +++ b/spec/lib/route_way_cost_unit_converter_spec.rb @@ -1,6 +1,7 @@ RSpec.describe RouteWayCostUnitConverter do describe ".convert" do - it "converts distance from meters to km and time from seconds to minutes" do + it "converts distance from meters to km and time from seconds to minutes " \ + "and rounds to two decimal places" do costs = { '1-2' => { 'distance' => 35223, @@ -16,11 +17,11 @@ RSpec.describe RouteWayCostUnitConverter do RouteWayCostUnitConverter.convert(costs) ).to eq({ '1-2' => { - 'distance' => 35.223, + 'distance' => 35.22, 'time' => 93 }, '94435-97513' => { - 'distance' => 35.919, + 'distance' => 35.92, 'time' => 102 } }) |
