在JavaScript中,我如何得到:
一个给定整数除另一个整数的整数倍是多少? 其余的呢?
在JavaScript中,我如何得到:
一个给定整数除另一个整数的整数倍是多少? 其余的呢?
当前回答
这里有一个方法。(就个人而言,我不会这样做,但认为这是一个有趣的方式来做的例子)上面提到的方式肯定是更好的,因为它调用多个函数,因此更慢,以及占用更多的空间在你的包。
函数intDivide(分子,分母){ 回归方法((分子/分母).toString () .split(“。”)[0]); } let x = intDivide(4,5); let y = intDivide(5,5); let z = intDivide(6,5); console.log (x); console.log (y); console.log (z);
其他回答
对于某个数y和某个除数x,计算商(商)[1]和余数(余)为:
const quotient = Math.floor(y/x);
const remainder = y % x;
例子:
const quotient = Math.floor(13/3); // => 4 => the times 3 fits into 13
const remainder = 13 % 3; // => 1
[1]由一个数除以另一个数得到的整数
这里有一个方法。(就个人而言,我不会这样做,但认为这是一个有趣的方式来做的例子)上面提到的方式肯定是更好的,因为它调用多个函数,因此更慢,以及占用更多的空间在你的包。
函数intDivide(分子,分母){ 回归方法((分子/分母).toString () .split(“。”)[0]); } let x = intDivide(4,5); let y = intDivide(5,5); let z = intDivide(6,5); console.log (x); console.log (y); console.log (z);
floor(operation)返回操作的四舍五入值。
第一个问题的例子:
Const x = 5; Const y = 10.4; const z =数学。地板(x + y); console.log (z);
第二个问题的例子:
Const x = 14; Const y = 5; const z =数学。地板(x % y); console.log (x);
const idivmod = (a, b) => [a/b |0, a%b];
还有一项关于这个问题的提案 模量和附加整数数学
function integerDivison(dividend, divisor){
this.Division = dividend/divisor;
this.Quotient = Math.floor(dividend/divisor);
this.Remainder = dividend%divisor;
this.calculate = ()=>{
return {Value:this.Division,Quotient:this.Quotient,Remainder:this.Remainder};
}
}
var divide = new integerDivison(5,2);
console.log(divide.Quotient) //to get Quotient of two value
console.log(divide.division) //to get Floating division of two value
console.log(divide.Remainder) //to get Remainder of two value
console.log(divide.calculate()) //to get object containing all the values