Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Fix floating point precision issue

Christophe Bliard914011d5600b
diff --git a/lib/chronic_duration.rb b/lib/chronic_duration.rb
index 1512154e0d2..5ba8baf7031 100644
--- a/lib/chronic_duration.rb
+++ b/lib/chronic_duration.rb
@@ -169,7 +169,7 @@ module ChronicDuration
dividers[:days] = "d" if days > 0
years = months = weeks = 0
- hours += (((minutes * 60) + seconds) / 3600.0).round(2)
+ hours = (hours + (((minutes * 60) + seconds) / 3600.0)).round(2)
hours_int = hours.to_i
hours = hours_int if hours - hours_int == 0 # if hours end with .0
minutes = seconds = 0
diff --git a/spec/services/duration_converter_spec.rb b/spec/services/duration_converter_spec.rb
index a20594a4e00..a4509d916dd 100644
--- a/spec/services/duration_converter_spec.rb
+++ b/spec/services/duration_converter_spec.rb
@@ -79,5 +79,10 @@ RSpec.describe DurationConverter do
expect(described_class.output(2.23))
.to eq("2.23h")
end
+
+ it "deals well with floating point maths" do
+ expect(described_class.output(1.89))
+ .to eq("1.89h")
+ end
end
end