Jump to content

javaScript integer rounded


Mudsaf

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.

Link to comment
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.

Link to comment
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
Link to comment
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)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...