Not able to use Math.ceil in Javascript

Avatar

Avatar
Validate 1
Level 1
ShagunMalik
Level 1

Like

1 like

Total Posts

55 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 1
Ignite 1
Give Back
View profile

Avatar
Validate 1
Level 1
ShagunMalik
Level 1

Like

1 like

Total Posts

55 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 1
Ignite 1
Give Back
View profile
ShagunMalik
Level 1

27-04-2021

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?

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

275 likes

Total Posts

320 posts

Correct reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

275 likes

Total Posts

320 posts

Correct reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
Anudeep_Garnepudi
MVP

27-04-2021

@ShagunMalik 

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

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

Answers (3)

Answers (3)

Avatar

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

645 likes

Total Posts

607 posts

Correct reply

239 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

645 likes

Total Posts

607 posts

Correct reply

239 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
BrianKasingli
MVP

27-04-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>

 

Avatar

Avatar
Applaud 25
Level 10
asutosh_jena
Level 10

Likes

593 likes

Total Posts

705 posts

Correct reply

207 solutions
Top badges earned
Applaud 25
Give Back 100
Boost 500
Affirm 100
Ignite 1
View profile

Avatar
Applaud 25
Level 10
asutosh_jena
Level 10

Likes

593 likes

Total Posts

705 posts

Correct reply

207 solutions
Top badges earned
Applaud 25
Give Back 100
Boost 500
Affirm 100
Ignite 1
View profile
asutosh_jena
Level 10

27-04-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! 

Avatar

Avatar
Springboard
Level 7
KiranVedantam1992
Level 7

Likes

195 likes

Total Posts

213 posts

Correct reply

60 solutions
Top badges earned
Springboard
Give Back 5
Ignite 1
Affirm 50
Validate 1
View profile

Avatar
Springboard
Level 7
KiranVedantam1992
Level 7

Likes

195 likes

Total Posts

213 posts

Correct reply

60 solutions
Top badges earned
Springboard
Give Back 5
Ignite 1
Affirm 50
Validate 1
View profile
KiranVedantam1992
Level 7

27-04-2021

Hi @ShagunMalik,

 

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

 

Thanks,

Kiran Vedantam