jQuery 数学 - 除以余数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14053696/
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-26 13:13:59  来源:igfitidea点击:

Math - Divide & leave remainder

jquerymath

提问by Shannon Hochkins

Possible Duplicate:
Integer division in JavaScript

可能的重复:
JavaScript 中的整数除法

Hopefully this is a simple question, basically I need to do this:

希望这是一个简单的问题,基本上我需要这样做:

divider = 15

分频器 = 15

number = 50

数量 = 50

Obviously 15 can be divided into 50 3 times with a remainder of 5, is there a simple way I can achieve this with math?

显然 15 可以分为 50 3 次,余数为 5,有没有一种简单的方法可以用数学来实现?

Obviously just dividing 50 by 15 will give me a rounded figure which I just want the lowest possible result and if there is anything left over and it's less than 15 just leave it alone.

显然,仅将 50 除以 15 会给我一个四舍五入的数字,我只想要尽可能低的结果,如果还有任何剩余并且小于 15,就不要管它。

Any help?

有什么帮助吗?

Cheers, Shannon

干杯,香农

EDIT:

编辑:

Thanks to Adil:

感谢阿迪尔:

x = 50;
y = 15;
res = x % y;
x = (x - res) / y;
// x = 3

回答by Adil

You can use modulus operator%to get remainder after division.

您可以使用模数运算符%来获得除法后的余数。

Live Demo

现场演示

remainder = 50 % 15;

回答by Hugo Delsing

To get the left over use the modulo. (division remainder)

要获得剩余部分,请使用模数。(除法余数)

x= 50 % 15