最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery division between two numbers - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Jul 31, 2012 at 18:42 Colin Brock 21.6k9 gold badges49 silver badges62 bronze badges asked Jul 31, 2012 at 18:41 southpaw93southpaw93 1,9615 gold badges23 silver badges40 bronze badges 4
  • 1 What exactly are you trying to do? Truncate the decimal? Round down? Round up? Perhaps just use Math.Floor: var z = Math.Floor(x/y); – AceCorban Commented Jul 31, 2012 at 18:43
  • 3 Somehow I'm reminded of this picture. – JJJ Commented Jul 31, 2012 at 18:43
  • @Juhana my thoughts too. – SomeKittens Commented Jul 31, 2012 at 18:43
  • $('jQuery').explosion(); – Mark Pieszak - Trilon.io Commented Jul 31, 2012 at 18:45
Add a comment  | 

3 Answers 3

Reset to default 13

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

Use simply parseInt() within javascript on the result

var x = 12.5;
var y = ​5;

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

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

发布评论

评论列表(0)

  1. 暂无评论