Not able to use Math.ceil in Javascript | Community
Skip to main content
Level 3
April 27, 2021
Solved

Not able to use Math.ceil in Javascript

  • April 27, 2021
  • 4 replies
  • 3259 views

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?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Anudeep_Garnepudi

@shagunmalik 

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

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

4 replies

Kiran_Vedantam
Community Advisor
Community Advisor
April 27, 2021

Hi @shagunmalik,

 

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

 

Thanks,

Kiran Vedantam

Level 3
April 27, 2021
Hi @kiran_vedantam, 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
Community Advisor
Anudeep_GarnepudiCommunity AdvisorAccepted solution
Community Advisor
April 27, 2021

@shagunmalik 

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

let xyz = 4.6; xyz = Math.ceil(xyz); console.log(xyz);
AG
Level 3
April 27, 2021
Thank you
Asutosh_Jena_
Community Advisor
Community Advisor
April 27, 2021

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! 

Level 3
April 27, 2021
Thank you.
BrianKasingli
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
April 27, 2021

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>