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] Always show one decimal for floats, fix formatting issue
Tobias Dillmann413908769171
diff --git a/app/models/custom_value/calculated_value_strategy.rb b/app/models/custom_value/calculated_value_strategy.rbindex f4481e31fe3..5f47edf22f0 100644--- a/app/models/custom_value/calculated_value_strategy.rb+++ b/app/models/custom_value/calculated_value_strategy.rb@@ -43,9 +43,18 @@ class CustomValue::CalculatedValueStrategy < CustomValue::FormatStrategyif integer_value?number_with_delimiter(value.to_i)else- number_with_delimiter(- number_with_precision(value.to_f, precision: 3, strip_insignificant_zeros: true)- )+ formatted = number_with_precision(value.to_f,+ precision: 3,+ strip_insignificant_zeros: true,+ delimiter: I18n.t("number.format.delimiter"),+ separator: I18n.t("number.format.separator"))++ # Ensure at least one decimal place for floats+ if formatted.exclude?(I18n.t("number.format.separator"))+ "#{formatted}#{I18n.t('number.format.separator')}0"+ else+ formatted+ endendend@@ -59,9 +68,6 @@ class CustomValue::CalculatedValueStrategy < CustomValue::FormatStrategyprivatedef integer_value?- # 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+ value =~ /\A[-+]?\d+\z/endenddiff --git a/app/models/custom_value/float_strategy.rb b/app/models/custom_value/float_strategy.rbindex bb8d49a1c62..feeef6b2713 100644--- a/app/models/custom_value/float_strategy.rb+++ b/app/models/custom_value/float_strategy.rb@@ -38,6 +38,8 @@ class CustomValue::FloatStrategy < CustomValue::FormatStrategyenddef formatted_value+ return "" if value.blank?+number_with_delimiter(value.to_s)enddiff --git a/spec/models/custom_value/calculated_value_strategy_spec.rb b/spec/models/custom_value/calculated_value_strategy_spec.rbindex a8bbef8dc16..d40969aad34 100644--- a/spec/models/custom_value/calculated_value_strategy_spec.rb+++ b/spec/models/custom_value/calculated_value_strategy_spec.rb@@ -66,15 +66,15 @@ RSpec.describe CustomValue::CalculatedValueStrategy dosubject { instance.formatted_value }context "when value is a float string" do- let(:value) { "3.14" }+ let(:value) { "32400.14" }- it "is the float string" do- expect(subject).to eql value+ it "is the float string with delimiters" do+ expect(subject).to eql "32,400.14"end- it "is localized" do+ it "is localized with delimiters" doI18n.with_locale(:de) do- expect(subject).to eql "3,14"+ expect(subject).to eql "32.400,14"endendend@@ -82,16 +82,38 @@ RSpec.describe CustomValue::CalculatedValueStrategy docontext "when value is a float string with many zeroed decimals" dolet(:value) { "3.0000000000000000000000000000009" }- it "is considered an integer" do- expect(subject).to eql "3"+ it "is formatted as a float with a minimum precision" do+ expect(subject).to eql "3.0"+ end+ end++ context "when value is a float string with some zeroed decimals" do+ let(:value) { "3.000" }++ it "is formatted as a float with a minimum precision" do+ expect(subject).to eql "3.0"+ end+ end++ context "when value is a float string with digits after the decimal point" do+ let(:value) { "42.87391" }++ it "is formatted as a float with a precision of 3 while rounding decimal places" do+ expect(subject).to eql "42.874"endendcontext "when value is an int string" do- let(:value) { "42" }+ let(:value) { "42312" }- it "is the int string" do- expect(subject).to eql value+ it "is the int string with delimiters" do+ expect(subject).to eql "42,312"+ end++ it "is localized with delimiters" do+ I18n.with_locale(:de) do+ expect(subject).to eql "42.312"+ endendend