The user enters 110 into the quantity, but since there are 20 pieces in the multiplicity, the quantity should automatically be recalculated upwards, it should be 120 pieces.
If, for example, the multiplicity is 100, and the client needs 1010 pieces, and he drives 1010 pieces into the quantity field, it should be recalculated to 1100 pieces.
I need rounding up depending on the multiplicity of goods.
curValue = Math.floor(Math.ceil((curValue * this.precisionFactor )/this.precisionFactor))
The simplest way, I think, to achieve this, is to first see how many 'batches' of size multiplicity there are, rounding this up to the nearest integer, and then multiplying this by the multiplicity again. That is
function multRound(amount, batchSize) {
// round up to whole number of batches
let numBatches = Math.ceil(amount/batchSize);
// return the number times the size of each
let total = numBatches * batchSize;
return total;
}
Of course you can quite easily make this a one-liner, but I thought it'd be clearer to write it like this.
var curValue = 105;
var multiplicity = 20;
var amountToAdd = 0;
for (var i = curValue; i >= multiplicity; i = i - multiplicity ) {
var temp = i - multiplicity;
amountToAdd = multiplicity - temp;
}
console.log("new value: " + (curValue + amountToAdd))
You can divide the value by multiplicity, and get the integer part using Math.floor() and add 1 to it (since you want it to be rounded up). Then return this value multiplied by multiplicity.
function round(currValue, multi){
return multi*(Math.floor(currValue/multi) + 1);
}
console.log(round(105, 20));
console.log(round(1010, 100));