如何将1个值从输入框除以10另一个输入框除以10而不四舍五入
How to Divide 1 value from input box to 10 another input box divided by 10 without rounding
我正在制作一个表格来将产品输入仓库,所以,我想要的条件是:
When I type and enter a value in Quantity box (Blue circle), I want that value shown to another 10 input boxes pallet list (red circle), But it has to be 69 Pcs in each box ( 69 is only total that actually fit on the pallet in real warehouse ) and if it still have rest, put the last rest in the last box. This is the illustration that might help :
说,我生产了 630 PCS 件,当我把 630 放入数量框时,应该显示 69PCS在每个pallet list中,69直到不能再分割为止。剩下的分界线,必须放在最后一个盒子上。
示例:
Quantity : 630 PCS
Pallet 1: 69
Pallet 2: 69
Pallet 3: 69
Pallet 4: 69
Pallet 5: 69
Pallet 6: 69
Pallet 7: 69
Pallet 8: 69
Pallet 9: 69
Pallet 10: 9 (The rest have to be on the last pallet)
有人可以帮助我吗?谢谢你,Terima Kasih。
let input = 630
let arr = []
for(let i=0;i<9;i++){
arr.push(0)
if(input>=69){
arr.push(69)
input-=69
}
}
arr.push(input)
不知道是不是你想要的
您需要模运算和整数除法。 JavaScript 中的 %
运算符可以很好地作为正数的模数。对于这个特定问题,您不需要对负数进行正确的模运算。整数除法是有底正态除法。 Math
对象提供了一个 floor
方法。
var
byId = document.getElementById.bind(document),
intVal = id => parseInt(byId(id).value),
onChange = ev =>
{
let
qty = intVal('quantity'),
ppb = intVal('peaces-per-box'),
rest = qty % ppb,
html = ''
;
if(!qty || !ppb)
return byId('pallets').innerHTML = '';
for (var c = Math.floor(qty / ppb), i = 0; i < c; i++)
html += `<div></div><lable for="pallet-${i}"></lable> <input id="pallet-${i}" value="${ppb}"></div>\n`;
if(rest)
html += `<div></div><lable for="pallet-${i}"></lable> <input id="pallet-${i}" value="${rest}"><div></div>\n`;
byId('pallets').innerHTML = html;
}
;
byId('quantity' ).addEventListener('change', onChange);
byId('peaces-per-box').addEventListener('change', onChange);
onChange();
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Quantities</title>
<body>
<form>
<div>
<lable for="quantity">quantity</lable>
<input id="quantity" type="number" min="0" step="1" placeholder="enter quantity">
</div>
<div>
<lable for="peaces-per-box">peaces per box</lable>
<input id="peaces-per-box" type="number" min="1" step="1" placeholder="enter max. peaces per box">
<div id="pallets">
</div>
</div>
</form>
我正在制作一个表格来将产品输入仓库,所以,我想要的条件是:
When I type and enter a value in Quantity box (Blue circle), I want that value shown to another 10 input boxes pallet list (red circle), But it has to be 69 Pcs in each box ( 69 is only total that actually fit on the pallet in real warehouse ) and if it still have rest, put the last rest in the last box. This is the illustration that might help :
说,我生产了 630 PCS 件,当我把 630 放入数量框时,应该显示 69PCS在每个pallet list中,69直到不能再分割为止。剩下的分界线,必须放在最后一个盒子上。
示例:
Quantity : 630 PCS
Pallet 1: 69
Pallet 2: 69
Pallet 3: 69
Pallet 4: 69
Pallet 5: 69
Pallet 6: 69
Pallet 7: 69
Pallet 8: 69
Pallet 9: 69
Pallet 10: 9 (The rest have to be on the last pallet)
有人可以帮助我吗?谢谢你,Terima Kasih。
let input = 630
let arr = []
for(let i=0;i<9;i++){
arr.push(0)
if(input>=69){
arr.push(69)
input-=69
}
}
arr.push(input)
不知道是不是你想要的
您需要模运算和整数除法。 JavaScript 中的 %
运算符可以很好地作为正数的模数。对于这个特定问题,您不需要对负数进行正确的模运算。整数除法是有底正态除法。 Math
对象提供了一个 floor
方法。
var
byId = document.getElementById.bind(document),
intVal = id => parseInt(byId(id).value),
onChange = ev =>
{
let
qty = intVal('quantity'),
ppb = intVal('peaces-per-box'),
rest = qty % ppb,
html = ''
;
if(!qty || !ppb)
return byId('pallets').innerHTML = '';
for (var c = Math.floor(qty / ppb), i = 0; i < c; i++)
html += `<div></div><lable for="pallet-${i}"></lable> <input id="pallet-${i}" value="${ppb}"></div>\n`;
if(rest)
html += `<div></div><lable for="pallet-${i}"></lable> <input id="pallet-${i}" value="${rest}"><div></div>\n`;
byId('pallets').innerHTML = html;
}
;
byId('quantity' ).addEventListener('change', onChange);
byId('peaces-per-box').addEventListener('change', onChange);
onChange();
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Quantities</title>
<body>
<form>
<div>
<lable for="quantity">quantity</lable>
<input id="quantity" type="number" min="0" step="1" placeholder="enter quantity">
</div>
<div>
<lable for="peaces-per-box">peaces per box</lable>
<input id="peaces-per-box" type="number" min="1" step="1" placeholder="enter max. peaces per box">
<div id="pallets">
</div>
</div>
</form>