简体   繁体   English

如何在 JavaScript 中将商作为 int 并将余数作为浮点

[英]How do I get the quotient as int and remainder as a floating point in JavaScript

On my calculator when I do 18/7 I get 2.5714285714285714285714285714286.在我的计算器上,当我做 18/7 时,我得到 2.5714285714285714285714285714286。 From my super limited Math skills 2 is the quotient and .5714285714285714285714285714286 is the remainder.从我的超级有限数学技能 2 是商和 .5714285714285714285714285714286 是余数。

How can I model this in JavaScript?如何在 JavaScript 中对此进行建模?

Thanks!谢谢!

var floatingPointPart = (18/7) % 1;
var integerPart = Math.floor(18/7);

Math.floor has the problem of rounding of the result to wrong direction in case of negative number it would be better to use bitwise operation to obtain interger quotients. 对于负数,Math.floor存在将结果舍入到错误方向的问题,使用逐位运算来获得整数商更好。

var quot = ~~(num/num1)

Hopefully this works for you! 希望这适合你!

var rawQuotient = 18/7;
var remainder = rawQuotient % 1;
var quotient = rawQuotient - remainder;

In most mathematics, there's no real need for distinction between the fractional portion and the whole portion, especially given that the whole thing can be expressed as a fraction (18/7ths), as a real number, or as a mix of integers and fractions (2 and 4/7ths). 在大多数数学中,没有真正需要区分小数部分和整个部分,特别是考虑到整个事物可以表示为分数(18/7),实数,或整数和分数的混合(2和4/7)。

When you get into programming or engineering, or some other derivative, you have definite uses for the separation; 当你进入编程或工程或其他衍生工具时,你有明确的分离用途; but the quotient is really the whole thing, integer and fraction, however you choose to represent that. 但商是真的整数,整数和分数,但你选择代表那个。

We can use simple mathematics to get answer using only / & % operator. 我们可以使用简单的数学来仅使用/%运算符来获得答案。

Consider 'num1' as first value & 'num2' as second value. 将'num1'视为第一个值,将'num2'视为第二个值。 Then : 然后 :

var Quotient = (num1 - (num1 % num2)) / num2;

var FloatingPoint = (num1 % num2) / num2;

2 is the quotient (or integer part of the result), 4 is the remainder, and 4/7 is the floating point part of the result, which the OP is requesting. 2是商(或结果的整数部分),4是余数,4/7是OP请求的结果的浮点部分。

var result = (18/7);
var integerPart = Math.floor(result);
var floatingPointPart = result - integerPart;

integerPart and floatingPointPart are the requested values. integerPart和floatingPointPart是请求的值。

var decimals = float - (float | 0);

Actually, 2 is the quotient and 4/18 is the remainder. 实际上,2是商,4/18是余数。

Math.divideby= function(d, dby){
    var q= Math.floor(d/dby), r= d-(q*dby);
    return r== 0? q:q+' and '+r+'/'+d;
}

Math.divideby(18,7) Math.divideby(18,7)

/* returned value: (String) */ / *返回值:(String)* /

2 and 4/18 2和4/18

In the decimal number 2.5714285714285716, the quotient , which also is the whole part , is 2 and the decimal / fractional part is 0.5714285714285716 or 0.5714285714285716 but actually, the remainder is 4.000000000000002 or 4在十进制数 2.5714285714285716 中,也是整数部分,是 2,小数/小数部分是 0.5714285714285716 或 0.5714285714285716 但实际上,余数是 00004 或 00004。

 let decimal = 18 / 7; let quotient = ~~decimal; let dec_remainder = decimal - quotient; let dec_remainder0 = decimal % 1; let remainder = dec_remainder * 7; let remainder0 = 18 % 7; document.body.innerHTML = "In the decimal number "+decimal+",<br> the quotient, which also is the whole part, is "+quotient+"<br>and the decimal/fractional part is "+dec_remainder+ " or " + dec_remainder0 + "<br>but actually, the remainder is "+remainder+" or "+remainder0 ;
 <!DOCTYPE html> <html> <body> </body> </html>

You can determine the quotient in several ways.您可以通过多种方式确定商。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM