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] 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.rb
index 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::FormatStrategy
if 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
+ end
end
end
@@ -59,9 +68,6 @@ class CustomValue::CalculatedValueStrategy < CustomValue::FormatStrategy
private
def 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/
end
end
diff --git a/app/models/custom_value/float_strategy.rb b/app/models/custom_value/float_strategy.rb
index 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::FormatStrategy
end
def formatted_value
+ return "" if value.blank?
+
number_with_delimiter(value.to_s)
end
diff --git a/spec/models/custom_value/calculated_value_strategy_spec.rb b/spec/models/custom_value/calculated_value_strategy_spec.rb
index 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 do
subject { 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" do
I18n.with_locale(:de) do
- expect(subject).to eql "3,14"
+ expect(subject).to eql "32.400,14"
end
end
end
@@ -82,16 +82,38 @@ RSpec.describe CustomValue::CalculatedValueStrategy do
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"
+ 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"
end
end
context "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"
+ end
end
end