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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[#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.rbindex 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::FormatStrategyprivatedef 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-9endenddiff --git a/spec/models/custom_value/calculated_value_strategy_spec.rb b/spec/models/custom_value/calculated_value_strategy_spec.rbindex 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 doendend+ 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" dolet(:value) { "42" }