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

[#67706] Format integer-like floats to look like ints

Tobias Dillmannfe97c8c678da
diff --git a/app/models/custom_value/calculated_value_strategy.rb b/app/models/custom_value/calculated_value_strategy.rb
index aadc40ee88c..f4481e31fe3 100644
--- a/app/models/custom_value/calculated_value_strategy.rb
+++ b/app/models/custom_value/calculated_value_strategy.rb
@@ -59,6 +59,9 @@ class CustomValue::CalculatedValueStrategy < CustomValue::FormatStrategy
private
def integer_value?
- value && value =~ /\A\d+\z/
+ # Use Epsilon comparison to determine whether the number, even if it is a float, should be
+ # considered an integer. E.g., a value of 42.000000000000000002 will be treated like an
+ # integer for our formatting purposes.
+ value && (value.to_f - value.to_i).abs < 1e-9
end
end
diff --git a/spec/models/custom_value/calculated_value_strategy_spec.rb b/spec/models/custom_value/calculated_value_strategy_spec.rb
index 03d232e5ecd..a8bbef8dc16 100644
--- a/spec/models/custom_value/calculated_value_strategy_spec.rb
+++ b/spec/models/custom_value/calculated_value_strategy_spec.rb
@@ -79,6 +79,14 @@ RSpec.describe CustomValue::CalculatedValueStrategy do
end
end
+ context "when value is a float string with many zeroed decimals" do
+ let(:value) { "3.0000000000000000000000000000009" }
+
+ it "is considered an integer" do
+ expect(subject).to eql "3"
+ end
+ end
+
context "when value is an int string" do
let(:value) { "42" }