mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
When creating new product via /admin/products/new use BigDecimal
to avoid bad rounded multiplication example here: 700*0.001 = 0.7000000000000001 + update specs as well
This commit is contained in:
@@ -32,7 +32,7 @@ angular.module("admin.products")
|
||||
if match
|
||||
$scope.product.master.unit_value = PriceParser.parse(match[1])
|
||||
$scope.product.master.unit_value = null if isNaN($scope.product.master.unit_value)
|
||||
$scope.product.master.unit_value *= $scope.product.variant_unit_scale if $scope.product.master.unit_value && $scope.product.variant_unit_scale
|
||||
$scope.product.master.unit_value = window.bigDecimal.multiply($scope.product.master.unit_value, $scope.product.variant_unit_scale, 2) if $scope.product.master.unit_value && $scope.product.variant_unit_scale
|
||||
$scope.product.master.unit_description = match[3]
|
||||
else
|
||||
value = $scope.product.master.unit_value
|
||||
|
||||
@@ -17,7 +17,8 @@ describe "unitsCtrl", ->
|
||||
inject ($rootScope, $controller, VariantUnitManager) ->
|
||||
scope = $rootScope
|
||||
ctrl = $controller 'unitsCtrl', {$scope: scope, VariantUnitManager: VariantUnitManager}
|
||||
|
||||
window.bigDecimal = jasmine.createSpyObj "bigDecimal", ["multiply"]
|
||||
window.bigDecimal.multiply.and.callFake (a, b, c) -> a * b
|
||||
|
||||
describe "interpretting variant_unit_with_scale", ->
|
||||
it "splits string with one underscore and stores the two parts", ->
|
||||
@@ -93,3 +94,11 @@ describe "unitsCtrl", ->
|
||||
scope.processUnitValueWithDescription()
|
||||
expect(scope.product.master.unit_value).toEqual 22.22
|
||||
expect(scope.product.master.unit_description).toEqual "things"
|
||||
|
||||
it "handles nice rounded division", ->
|
||||
# this is a bit absurd, but it assure use that bigDecimal is called
|
||||
window.bigDecimal.multiply.and.returnValue 0.7
|
||||
scope.product.master.unit_value_with_description = "700"
|
||||
scope.product.variant_unit_scale = 0.001
|
||||
scope.processUnitValueWithDescription()
|
||||
expect(scope.product.master.unit_value).toEqual 0.7
|
||||
|
||||
Reference in New Issue
Block a user