00 Votes

JavaScript: Round Number - round() not working

Question by Guest | 2015-05-05 at 18:24

Is there any simple possibility to round numbers in JavaScript? For example, a function that is able to convert the floating-point number 2.3 to the number 2?

I have already tried to call the function round(), but that is not working for me. On the Internet, I have found some confusing instructions not helping me at all. Isn't there any easy solution?

ReplyPositiveNegative
0Best Answer0 Votes

With just calling round(), it is not working. Instead, you have to take Math.round().

Here is a small example:

var k = 10 / 4;  

var r = Math.round(k);

The result of 10 / 4 is 2.5. We are rounding this result and we are getting 3.

Note: A value of 2.49 will be rounded down, a value of 2.5 will be rounded up.
2015-05-06 at 14:34

ReplyPositive Negative
Reply

Related Topics

PHP: Rounding Numbers

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.