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

Not able to use Math.ceil in Javascript

Avatar

Avatar
Validate 1
Level 1
ShagunMalik
Level 1

Like

1 like

Total Posts

27 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

27 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

271 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

271 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

582 likes

Total Posts

564 posts

Correct Reply

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

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

582 likes

Total Posts

564 posts

Correct Reply

218 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
Affirm 100
Level 10
asutosh_jena
Level 10

Likes

373 likes

Total Posts

480 posts

Correct Reply

133 solutions
Top badges earned
Affirm 100
Ignite 1
Establish
Give Back 50
Give Back 5
View profile

Avatar
Affirm 100
Level 10
asutosh_jena
Level 10

Likes

373 likes

Total Posts

480 posts

Correct Reply

133 solutions
Top badges earned
Affirm 100
Ignite 1
Establish
Give Back 50
Give Back 5
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
Validate 1
Level 6
KiranVedantam1992
Level 6

Likes

150 likes

Total Posts

155 posts

Correct Reply

44 solutions
Top badges earned
Validate 1
Establish
Give Back 3
Give Back
Boost 50
View profile

Avatar
Validate 1
Level 6
KiranVedantam1992
Level 6

Likes

150 likes

Total Posts

155 posts

Correct Reply

44 solutions
Top badges earned
Validate 1
Establish
Give Back 3
Give Back
Boost 50
View profile
KiranVedantam1992
Level 6

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