javascript 两个数字之间的jquery除法

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

Jquery division between two numbers

javascript

提问by southpaw93

Can you guys please help me in this matter :

你们能帮我解决这个问题吗:

I have two variable in jquery x and y , and I am doing a division between those two , and I let's say the result is 2.5 , but I only want to show 2 from 2.5 , how do I do that?

我在 jquery x 和 y 中有两个变量,我正在对这两个变量进行除法,假设结果是 2.5 ,但我只想显示 2.5 中的 2 ,我该怎么做?

回答by jeff

If you're simply trying to truncate the decimal, use the following:

如果您只是想截断小数,请使用以下命令:

parseInt(2.5); // returns 2

If you're trying to round to the nearest integer, use the following:

如果您尝试四舍五入到最接近的整数,请使用以下命令:

Math.round(2.5); // returns 3

If you're trying to round down, use the following:

如果您要四舍五入,请使用以下命令:

Math.floor(2.5); // returns 2

If you're trying to round up, use the following:

如果您要四舍五入,请使用以下命令:

Math.ceil(2.5); // returns 3

回答by Mark Pieszak - Trilon.io

Use simply parseInt() within javascript on the result

在结果的 javascript 中简单地使用 parseInt()

var x = 12.5;
var y = ?5;

?var result = parseInt(x / y, 10); // the 2nd parameter signifies base-10
alert(result);?

回答by SomeKittens

You don't need to use jQuery at all for this. Simply use JavaScript's parseInt()method.

您根本不需要为此使用 jQuery。只需使用 JavaScript 的parseInt()方法。