mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Avoid rounded with float numbers errors with javascript
0.7/0.001 = 699.9999999999999 Use Math.round() to avoid this.
This commit is contained in:
@@ -48,7 +48,7 @@ angular.module("admin.products").factory "OptionValueNamer", (VariantUnitManager
|
||||
option_value_value_unit_scaled: ->
|
||||
[unit_scale, unit_name] = @scale_for_unit_value()
|
||||
|
||||
value = @variant.unit_value / unit_scale
|
||||
value = Math.round((@variant.unit_value / unit_scale) * 100) / 100
|
||||
|
||||
[value, unit_name]
|
||||
|
||||
|
||||
@@ -97,6 +97,13 @@ describe "Option Value Namer", ->
|
||||
p.variant_unit_scale = scale
|
||||
v.unit_value = 100 * scale
|
||||
expect(namer.option_value_value_unit()).toEqual [100, unit]
|
||||
|
||||
it "generates right values for volume with rounded values", ->
|
||||
unit = 'L'
|
||||
p.variant_unit = 'volume'
|
||||
p.variant_unit_scale = 1.0
|
||||
v.unit_value = 0.7
|
||||
expect(namer.option_value_value_unit()).toEqual [700, 'mL']
|
||||
|
||||
it "chooses the correct scale when value is very small", ->
|
||||
p.variant_unit = 'volume'
|
||||
|
||||
Reference in New Issue
Block a user