# javaScript integer rounded

## Recommended Posts

Hello, i'm wondering which method i need to use tho get these values below working.

`1 //This should return 11.2 //This should return 21.00001 //This should return 12.5 //This should return 33 //This should return 3`

and so on, i guess you'll get my point. I've see that Math.round(); fixes it like decimal should work, but i want the number to go rounded to next higher number.

##### Share on other sites

Math.ceil() will round up to the next number, but 1.00001 will round up to 2 as well. It's up to you to decide a threshold and create a function that chooses Math.ceil() or Math.floor() depending on what value it has.

##### Share on other sites

LOL

`function rnd(u){if(u==1) {return 1}else if(u==1.2) {return 2}else if(u==1.00001) {return 1}else if(u==2.5) {return 3}else if(u==3) {return 3}else {return "this is a code joke!"}}`
Edited by davej
##### Share on other sites

Math.ceil() will round up to the next number, but 1.00001 will round up to 2 as well. It's up to you to decide a threshold and create a function that chooses Math.ceil() or Math.floor() depending on what value it has.

Thanks

LOL

`function rnd(u){if(u==1) {return 1}else if(u==1.2) {return 2}else if(u==1.00001) {return 1}else if(u==2.5) {return 3}else if(u==3) {return 3}else {return "this is a code joke!"}}`

Lol x)

##### Share on other sites

Now seriously -- such a function can do whatever you might want -- as long as what you want makes sense. You would probably need to also make use of ceil(), floor(), or round(), or some combination of them.

http://www.w3schools.com/jsref/jsref_obj_math.asp