+ 8
what is the difference between Math.floor, Math.round, Math.ceil in JavaScript?
I'm completely new to JavaScript. I'm confused to determine the difference between these Math Objects. Hope you'll help me & thanks in advance...
11 Antworten
+ 24
Let's take a a value 2.3
So math floor objects are used round a particular value downwards to the nearest integer.
• so Math.floor(2.3) gives the value 2.0
[if we take a value 2.8 then also Math.floor(2.3) is 2]
Math.round() rounds a particular number to the nearest integer.
•So Math.round(2.3) gives 2 as a a result.
[If we have a value 7.8 then Math.round(7.8) will be 8] - it will round to the nearest integer
Math.ceil() rounds of a particular number upwards to the nearest integer.
• Math.ceil(2.3) gives the result as 3
[ If we take a value 4.9 then Math.ceil(4.9) gives a value 5]
Must be more than the actual value.
More examples -
1. Math.ceil(2.3) = 3
2. Math.round(2.4)=2
3.Math.round(4.7)=5
4.Math.floor(2.7)=2
5.Math.floor(3.1)=3
6.Math.ceil(6.8) = 7
+ 14
Ok Abir Sheikh
Math.round just round the value.
Means if decimal will be 4 or less than 4 it will be round down.
Ex. Math.round(3.2) == 3
Math.round(3.4) == 3
If decimal will be 5 or greater than 5 then it will be round up.
Ex. Math.round(3.5) == 4
Math.round(3.8) == 5
Lets take more examples of math.round.
8.1 == 8
8.2 == 8
8.3 == 8
8.4 == 8
8.5 == 9
8.6 == 9
8.7 == 9
8.8 == 9
8.9 == 9
Hope now you will understand
+ 11
Math.floor rounds down the value.
Ex. Math.floor(5.8) == 5
Math.round rounds the value.
Ex. Math.round(5.4) == 5
Math.round(5.7) == 6
Math.ceil rounds up the value.
Ex. Math.ceil(5.2) == 6
Hope you will understand
+ 8
You can reach this
https://www.sololearn.com/learn/JavaScript/1243/?ref=app
+ 2
Abir Sheikh dost it is very simple.
Lets learn with an example.
1. Math.floor
It return previous number then actual number when the number is in decimal.
Eg. Math.floor (5.3) ==5
Eg. Math.floor (5.9) == 5
2. Math.round
It rounds value, it returns next number then the actual when decimals is more than or equal to 0.5
Eg. Math.round(5.5) ==6
Eg. Math.round (5.8) == 6
It return previous number then the actual number when decimals is less or equal to 0.49.
Eg. Math.round (5.4) ==4
Eg. Math.round (5.2) == 4
3. Math.ceil
It returns next number then the actual number when the number is in decimal.
Eg. Math.ceil(5.3) ==6
Eg.Math.ceil (5.7) ==6
I hope you understood. Plz tell me if still have doubt
+ 1
Nilesh please explain in words...
I'm confused cause, they look like same 🙄🙄🙄
+ 1
Priyanshi💞 sorry, but I didn't😓😓😓...
I understood Math.floor & Math.ceil but I didn't understand Math.round !
+ 1
Priyanshi💞 Nilesh Thanks A Lot...
+ 1
Himanshu Shah Yes Bro, It's clear now..
Thanks 😊😊😊
0
Math.floor truncates the figure while math.round doe the usual rounding off
- 1
leer las preguntas