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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12538820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 10:39:21  来源:igfitidea点击:

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

javascriptmath

提问by Sebastian Patten

On my calculator when I do 18/7 I get 2.5714285714285714285714285714286. From my super limited Math skills 2 is the quotient and .5714285714285714285714285714286 is the remainder.

在我的计算器上,当我做 18/7 时,我得到 2.5714285714285714285714285714286。从我的超级有限数学技能 2 是商和 .5714285714285714285714285714286 是余数。

How can I model this in JavaScript?

如何在 JavaScript 中对此进行建模?

Thanks!

谢谢!

回答by Zéychin

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

回答by Norguard

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/7ths)、实数或整数和分数的混合(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.

当您进入编程或工程或其他一些衍生产品时,您对分离有明确的用途;但商实际上是整个事物,整数和分数,无论您选择如何表示。

回答by Jibin Mathew

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!

希望这对你有用!

回答by Atanu Pal

We can use simple mathematics to get answer using only /& %operator.

我们可以使用简单的数学来仅使用/&%运算符来获得答案。

Consider 'num1' as first value & 'num2' as second value. Then :

将“num1”视为第一个值,将“num2”视为第二个值。然后 :

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

var FloatingPoint = (num1 % num2) / num2;

回答by Sharon Minsuk

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 是请求的值。

回答by hobberwickey

var decimals = float - (float | 0);

回答by kennebec

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) */

/* 返回值:(字符串)*/

2 and 4/18

2 和 4/18