4

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?

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); Commented Jul 31, 2012 at 18:43
  • 3
    Somehow I'm reminded of this picture. Commented Jul 31, 2012 at 18:43
  • @Juhana my thoughts too. Commented Jul 31, 2012 at 18:43
  • $('jQuery').explosion(); Commented Jul 31, 2012 at 18:45

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

2

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);​

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.