Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

Not able to use Math.ceil in Javascript

ShagunMalik
Level 3
Level 3

I'm using Onready function in which I'm calling a function say func();

In func() I'm using Math.ceil on a decimal value stored in a variable, but it is not working. But if I use Math.ceil in console on same variable then it is working fine.

How to use it in my function?

1 Accepted Solution
Anudeep_Garnepudi
Correct answer by
Community Advisor
Community Advisor

@ShagunMalik 

You should assign the Math.ceil(..) returned value to a variable.

let xyz = 4.6;
xyz = Math.ceil(xyz);
console.log(xyz);

View solution in original post

7 Replies
KiranVedantam1992
Community Advisor
Community Advisor

Hi @ShagunMalik,

 

What is the error that you are seeing in the console? Can you please share the log?

 

Thanks,

Kiran Vedantam

ShagunMalik
Level 3
Level 3
Hi @KiranVedantam1992, there is no error, it is that like if I want to round off 4.6 to 5 using Math.ceil then it is not working, It is showing 4.6 only but If I type the same code in console then it is working fine and showing 5.
Anudeep_Garnepudi
Correct answer by
Community Advisor
Community Advisor

@ShagunMalik 

You should assign the Math.ceil(..) returned value to a variable.

let xyz = 4.6;
xyz = Math.ceil(xyz);
console.log(xyz);

View solution in original post

asutosh_jena
Community Advisor
Community Advisor

Hi @ShagunMalik 

 

You can use Math.ceil inside a function like below:

$(document).ready(function () {
alert(someName());
});


function someName() {
var x = 0.95;
return Math.ceil(x);
}

 

On page load it will give a value of 1 as alert.

 

And the below one gives as 5.

 

$(document).ready(function () {
alert(someName());
});


function someName() {
var x = 4.6;
return Math.ceil(x);
}

 

Thanks! 

BrianKasingli
Community Advisor
Community Advisor

Instead of calling your function from onReady, try just directly adding this line of code in your HTML document:

 

<script>
console.log(Math.ceil(-7.004));
</script>