mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user