Pages

Wednesday, March 6, 2013

Print the next highest integer value by rounding up value with javascript

To convert a decimal value like 4.5 to a round value to its nearest decimal integer we have three main function in the javascript library

Math.ceil() = Round a number upward to it's nearest integer:
Math.round() = Round a number to the nearest integer:
Math.floor() = Round a number downward to its nearest integer:



Round() example

var a=Math.round(2.60);
var b=Math.round(2.50);
var c=Math.round(2.49);
var d=Math.round(-2.60);
var e=Math.round(-2.50);
var f=Math.round(-2.49);

Result

3
3
2
-3
-2
-2


Ceil() example

var a=Math.ceil(0.60);
var b=Math.ceil(0.40);
var c=Math.ceil(5);
var d=Math.ceil(5.1);
var e=Math.ceil(-5.1);
var f=Math.ceil(-5.9);

Result

1
1
5
6
-5
-5


floor() example

var a=Math.floor(0.60);
var b=Math.floor(0.40);
var c=Math.floor(5);
var d=Math.floor(5.1);
var e=Math.floor(-5.1);
var f=Math.floor(-5.9);

Result

0
0
5
5
-6
-6

No comments:

About Me

Hi,My name is Harish Kumar.I am a web developer by profession.I am working in the same field since 2008. About my skills i am proficient in HTML,CSS,JAVASCRIPT/JQUERY/ ANGULAR,PHP,MYSQL,APACHE,LINUXAJAX, REST,etc...