Truncate scaled value unit to maximum of two decimals

ie truncate 12.50001234 to 12.5

When using imperial, the scalling calculation rounding results in value unit
having extra decimal when converted back to imperial

+ related spec
This commit is contained in:
Gaetan Craig-Riou
2023-01-11 10:49:27 +11:00
parent c4b89e466b
commit 91af282ccc
2 changed files with 10 additions and 1 deletions

View File

@@ -56,7 +56,7 @@ module VariantUnits
def option_value_value_unit_scaled
unit_scale, unit_name = scale_for_unit_value
value = BigDecimal(@variant.unit_value / unit_scale, 6)
value = BigDecimal(@variant.unit_value / unit_scale, 6).truncate(2)
[value, unit_name]
end

View File

@@ -145,6 +145,15 @@ module VariantUnits
allow(v).to receive(:unit_value) { nil }
expect(subject.send(:option_value_value_unit)).to eq [nil, nil]
end
it "truncates value to 2 decimals maximum" do
oz_scale = 28.35
p = double(:product, variant_unit: 'weight', variant_unit_scale: oz_scale)
allow(v).to receive(:product) { p }
# The unit_value is stored rounded to 2 decimals
allow(v).to receive(:unit_value) { (12.5 * oz_scale).round(2) }
expect(subject.send(:option_value_value_unit)).to eq [BigDecimal(12.5, 6), 'oz']
end
end
end
end