Help About math.ceil / math.floor

Hi, im a newbie scripter and im trying learn about
math function. especcialy math.ceil or math.floor

i made a system before that rounds seconds
like if second is 45.7 it rounds it to 46 etc.
but i used a lot of if / elseif staircase to do that.
i want to learn how to do it with math.ceil and math.floor
can you guys show me how?

my current code :

	if MaxTimesec > 1.5 and MaxTimesec < 2 then
		MaxTimesec = 2 
	else if MaxTimesec < 1.5 and MaxTimesec > 1 then
			MaxTimesec = 1
		else if MaxTimesec < 0.5 and MaxTimesec > 0 then
				MaxTimesec = 0
			else if MaxTimesec > 0.5 and MaxTimesec < 1 then
					MaxTimesec = 1
				else if MaxTimesec > 2.5 and MaxTimesec < 3 then
						MaxTimesec = 3
					else if MaxTimesec < 2.5 and MaxTimesec > 2 then
							MaxTimesec = 2
						else if MaxTimesec > 3.5 and MaxTimesec < 4 then
								MaxTimesec = 4
							else if MaxTimesec < 3.5 and MaxTimesec > 3 then
									MaxTimesec = 3
								else if MaxTimesec > 4.5 and MaxTimesec < 5 then
										MaxTimesec = 5
									else if MaxTimesec < 4.5 and MaxTimesec > 4 then
											MaxTimesec = 4
										else if MaxTimesec > 5.5 and MaxTimesec < 6 then
												MaxTimesec = 6
											else if MaxTimesec < 5.5 and MaxTimesec > 5 then
													MaxTimesec = 5
												else if MaxTimesec > 6.5 and MaxTimesec < 7 then
														MaxTimesec = 7
													else if MaxTimesec < 6.5 and MaxTimesec > 6 then
															MaxTimesec = 6
														else if MaxTimesec > 7.5 and MaxTimesec < 8 then
																MaxTimesec = 8
															else if MaxTimesec < 7.5 and MaxTimesec > 7 then
																	MaxTimesec = 7
																else if MaxTimesec > 8.5 and MaxTimesec < 9 then
																		MaxTimesec = 9
																	else if MaxTimesec < 8.5 and MaxTimesec > 8 then
																			MaxTimesec = 8
																		else if MaxTimesec > 9.5 and MaxTimesec < 10 then
																				MaxTimesec = 10
																			else if MaxTimesec < 9.5 and MaxTimesec > 9 then
																					MaxTimesec = 9
																					print(MaxTimesec)
																				else if MaxTimesec > 10.5 and MaxTimesec < 11 then
																						MaxTimesec = 11
																					else if MaxTimesec < 10.5 and MaxTimesec > 10 then
																							MaxTimesec = 10


																						else if MaxTimesec < 11.5 and MaxTimesec > 11 then
																								MaxTimesec = 11

																							else if MaxTimesec > 12.5 and MaxTimesec < 13 then
																									MaxTimesec = 13
																								else if MaxTimesec < 12.5 and MaxTimesec > 12 then
																										MaxTimesec = 12
																									else if MaxTimesec > 13.5 and MaxTimesec < 14 then
																											MaxTimesec = 14
																										else if MaxTimesec < 13.5 and MaxTimesec > 13 then
																												MaxTimesec = 13
																											else if MaxTimesec > 14.5 and MaxTimesec < 15 then
																													MaxTimesec = 15
																												else if MaxTimesec < 14.5 and MaxTimesec > 14 then
																														MaxTimesec = 14
																													else if MaxTimesec > 15.5 and MaxTimesec < 16 then
																															MaxTimesec = 16
																														else if MaxTimesec < 15.5 and MaxTimesec > 15 then
																																MaxTimesec = 15
																															else if MaxTimesec > 16.5 and MaxTimesec < 17 then
																																	MaxTimesec = 17
																																else if MaxTimesec < 16.5 and MaxTimesec > 16 then
																																		MaxTimesec = 16
																																	else if MaxTimesec > 17.5 and MaxTimesec < 18 then
																																			MaxTimesec = 18
																																		else if MaxTimesec < 17.5 and MaxTimesec > 17 then
																																				MaxTimesec = 17
																																			else if MaxTimesec > 18.5 and MaxTimesec < 19 then
																																					MaxTimesec = 19
																																				else if MaxTimesec < 18.5 and MaxTimesec > 18 then
																																						MaxTimesec = 18
																																					else if MaxTimesec > 19.5 and MaxTimesec < 20 then
																																							MaxTimesec = 20
																																						else if MaxTimesec < 19.5 and MaxTimesec > 19 then
																																								MaxTimesec = 19
																																							else if MaxTimesec > 20.5 and MaxTimesec < 21 then
																																									MaxTimesec = 21
																																								else if MaxTimesec < 20.5 and MaxTimesec > 20 then
																																										MaxTimesec = 20

																																									else if MaxTimesec < 21.5 and MaxTimesec > 21 then
																																											MaxTimesec = 21

																																										else if MaxTimesec > 22.5 and MaxTimesec < 23 then
																																												MaxTimesec = 23
																																											else if MaxTimesec < 22.5 and MaxTimesec > 22 then
																																													MaxTimesec = 22
																																												else if MaxTimesec > 23.5 and MaxTimesec < 24 then
																																														MaxTimesec = 24
																																													else if MaxTimesec < 23.5 and MaxTimesec > 23 then
																																															MaxTimesec = 23
																																														else if MaxTimesec > 24.5 and MaxTimesec < 25 then
																																																MaxTimesec = 25
																																															else if MaxTimesec < 24.5 and MaxTimesec > 24 then
																																																	MaxTimesec = 24
																																																else if MaxTimesec > 25.5 and MaxTimesec < 26 then
																																																		MaxTimesec = 26
																																																	else if MaxTimesec < 25.5 and MaxTimesec > 25 then
																																																			MaxTimesec = 25
																																																		else if MaxTimesec > 26.5 and MaxTimesec < 27 then
																																																				MaxTimesec = 27
																																																			else if MaxTimesec < 26.5 and MaxTimesec > 26 then
																																																					MaxTimesec = 26
																																																				else if MaxTimesec > 27.5 and MaxTimesec < 28 then
																																																						MaxTimesec = 28
																																																					else if MaxTimesec < 27.5 and MaxTimesec > 27 then
																																																							MaxTimesec = 27
																																																						else if MaxTimesec > 28.5 and MaxTimesec < 29 then
																																																								MaxTimesec = 29
																																																							else if MaxTimesec < 28.5 and MaxTimesec > 28 then
																																																									MaxTimesec = 28
																																																								else if MaxTimesec > 29.5 and MaxTimesec < 30 then
																																																										MaxTimesec = 30
																																																									else if MaxTimesec < 29.5 and MaxTimesec > 29 then
																																																											MaxTimesec = 29
																																																										else if MaxTimesec > 30.5 and MaxTimesec < 31 then
																																																												MaxTimesec = 31
																																																											else if MaxTimesec < 30.5 and MaxTimesec > 30 then
																																																													MaxTimesec = 30

																																																												else if MaxTimesec < 31.5 and MaxTimesec > 31 then
																																																														MaxTimesec = 31

																																																													else if MaxTimesec > 32.5 and MaxTimesec < 33 then
																																																															MaxTimesec = 33
																																																														else if MaxTimesec < 32.5 and MaxTimesec > 32 then
																																																																MaxTimesec = 32
																																																															else if MaxTimesec > 33.5 and MaxTimesec < 34 then
																																																																	MaxTimesec = 34
																																																																else if MaxTimesec < 33.5 and MaxTimesec > 33 then
																																																																		MaxTimesec = 33
																																																																	else if MaxTimesec > 34.5 and MaxTimesec < 35 then
																																																																			MaxTimesec = 35
																																																																		else if MaxTimesec < 34.5 and MaxTimesec > 34 then
																																																																				MaxTimesec = 34
																																																																			else if MaxTimesec > 35.5 and MaxTimesec < 36 then
																																																																					MaxTimesec = 36
																																																																				else if MaxTimesec < 35.5 and MaxTimesec > 35 then
																																																																						MaxTimesec = 35
																																																																					else if MaxTimesec > 36.5 and MaxTimesec < 37 then
																																																																							MaxTimesec = 37
																																																																						else if MaxTimesec < 36.5 and MaxTimesec > 36 then
																																																																								MaxTimesec = 36
																																																																							else if MaxTimesec > 37.5 and MaxTimesec < 38 then
																																																																									MaxTimesec = 38
																																																																								else if MaxTimesec < 37.5 and MaxTimesec > 37 then
																																																																										MaxTimesec = 37
																																																																									else if MaxTimesec > 38.5 and MaxTimesec < 39 then
																																																																											MaxTimesec = 39
																																																																										else if MaxTimesec < 38.5 and MaxTimesec > 38 then
																																																																												MaxTimesec = 38
																																																																											else if MaxTimesec > 39.5 and MaxTimesec < 40 then
																																																																													MaxTimesec = 40
																																																																												else if MaxTimesec < 39.5 and MaxTimesec > 39 then
																																																																														MaxTimesec = 39
																																																																													else if MaxTimesec > 40.5 and MaxTimesec < 41 then
																																																																															MaxTimesec = 41
																																																																														else if MaxTimesec < 40.5 and MaxTimesec > 40 then
																																																																																MaxTimesec = 40

																																																																															else if MaxTimesec < 41.5 and MaxTimesec > 41 then
																																																																																	MaxTimesec = 41

																																																																																else if MaxTimesec > 42.5 and MaxTimesec < 43 then
																																																																																		MaxTimesec = 43
																																																																																	else if MaxTimesec < 42.5 and MaxTimesec > 42 then
																																																																																			MaxTimesec = 42
																																																																																		else if MaxTimesec > 43.5 and MaxTimesec < 44 then
																																																																																				MaxTimesec = 44
																																																																																			else if MaxTimesec < 43.5 and MaxTimesec > 43 then
																																																																																					MaxTimesec = 43
																																																																																				else if MaxTimesec > 44.5 and MaxTimesec < 45 then
																																																																																						MaxTimesec = 45
																																																																																					else if MaxTimesec < 44.5 and MaxTimesec > 44 then
																																																																																							MaxTimesec = 44
																																																																																						else if MaxTimesec > 45.5 and MaxTimesec < 46 then
																																																																																								MaxTimesec = 46
																																																																																							else if MaxTimesec < 45.5 and MaxTimesec > 45 then
																																																																																									MaxTimesec = 45
																																																																																								else if MaxTimesec > 46.5 and MaxTimesec < 47 then
																																																																																										MaxTimesec = 47
																																																																																									else if MaxTimesec < 46.5 and MaxTimesec > 46 then
																																																																																											MaxTimesec = 46
																																																																																										else if MaxTimesec > 47.5 and MaxTimesec < 48 then
																																																																																												MaxTimesec = 48
																																																																																											else if MaxTimesec < 47.5 and MaxTimesec > 47 then
																																																																																													MaxTimesec = 47
																																																																																												else if MaxTimesec > 48.5 and MaxTimesec < 49 then
																																																																																														MaxTimesec = 49
																																																																																													else if MaxTimesec < 48.5 and MaxTimesec > 48 then
																																																																																															MaxTimesec = 48
																																																																																														else if MaxTimesec > 49.5 and MaxTimesec < 50 then
																																																																																																MaxTimesec = 50
																																																																																															else if MaxTimesec < 49.5 and MaxTimesec > 49 then
																																																																																																	MaxTimesec = 49
																																																																																																else if MaxTimesec > 50.5 and MaxTimesec < 51 then
																																																																																																		MaxTimesec = 51
																																																																																																	else if MaxTimesec < 50.5 and MaxTimesec > 50 then
																																																																																																			MaxTimesec = 50

																																																																																																		else if MaxTimesec < 51.5 and MaxTimesec > 51 then
																																																																																																				MaxTimesec = 51

																																																																																																			else if MaxTimesec > 52.5 and MaxTimesec < 53 then
																																																																																																					MaxTimesec = 53
																																																																																																				else if MaxTimesec < 52.5 and MaxTimesec > 52 then
																																																																																																						MaxTimesec = 52
																																																																																																					else if MaxTimesec > 53.5 and MaxTimesec < 54 then
																																																																																																							MaxTimesec = 54
																																																																																																						else if MaxTimesec < 53.5 and MaxTimesec > 53 then
																																																																																																								MaxTimesec = 53
																																																																																																							else if MaxTimesec > 54.5 and MaxTimesec < 55 then
																																																																																																									MaxTimesec = 55
																																																																																																								else if MaxTimesec < 54.5 and MaxTimesec > 54 then
																																																																																																										MaxTimesec = 54
																																																																																																									else if MaxTimesec > 55.5 and MaxTimesec < 56 then
																																																																																																											MaxTimesec = 56
																																																																																																										else if MaxTimesec < 55.5 and MaxTimesec > 55 then
																																																																																																												MaxTimesec = 55
																																																																																																											else if MaxTimesec > 56.5 and MaxTimesec < 57 then
																																																																																																													MaxTimesec = 57
																																																																																																												else if MaxTimesec < 56.5 and MaxTimesec > 56 then
																																																																																																														MaxTimesec = 56
																																																																																																													else if MaxTimesec > 57.5 and MaxTimesec < 58 then
																																																																																																															MaxTimesec = 58
																																																																																																														else if MaxTimesec < 57.5 and MaxTimesec > 57 then
																																																																																																																MaxTimesec = 57
																																																																																																															else if MaxTimesec > 58.5 and MaxTimesec < 59 then
																																																																																																																	MaxTimesec = 59
																																																																																																																else if MaxTimesec < 58.5 and MaxTimesec > 58 then
																																																																																																																		MaxTimesec = 58
																																																																																																																	end
																																																																																																																end
																																																																																																															end
																																																																																																														end
																																																																																																													end
																																																																																																												end
																																																																																																											end
																																																																																																										end
																																																																																																									end
																																																																																																								end
																																																																																																							end
																																																																																																						end
																																																																																																					end
																																																																																																				end
																																																																																																			end
																																																																																																		end
																																																																																																	end
																																																																																																end
																																																																																															end
																																																																																														end
																																																																																													end
																																																																																												end
																																																																																											end
																																																																																										end
																																																																																									end
																																																																																								end
																																																																																							end
																																																																																						end
																																																																																					end
																																																																																				end
																																																																																			end
																																																																																		end
																																																																																	end
																																																																																end
																																																																															end
																																																																														end
																																																																													end
																																																																												end
																																																																											end
																																																																										end
																																																																									end
																																																																								end
																																																																							end
																																																																						end
																																																																					end
																																																																				end
																																																																			end
																																																																		end
																																																																	end
																																																																end
																																																															end
																																																														end
																																																													end
																																																												end
																																																											end
																																																										end
																																																									end
																																																								end
																																																							end
																																																						end
																																																					end
																																																				end
																																																			end
																																																		end
																																																	end
																																																end
																																															end
																																														end
																																													end
																																												end
																																											end
																																										end
																																									end
																																								end
																																							end
																																						end
																																					end
																																				end
																																			end
																																		end
																																	end
																																end
																															end
																														end
																													end
																												end
																											end
																										end
																									end
																								end
																							end
																						end
																					end
																				end
																			end
																		end
																	end
																end
															end
														end
													end
												end
											end
										end
									end
								end
							end
						end
					end
				end
			end
		end
	end
1 Like

math.ceil returns the closest integer larger than or equal to the number you have;
math.floor returns the closest inte below or equal to the number you have.

You can use math.round in your situation, though - it would return you an integer closest to the number you insert.

Refer to this article for more information about math functions.

1 Like

Something that may be helpful to you in the future: elseif is one statement. You’ve created a staircase because you’re spacing out else and if which creates a completely different statement and even gets evaluated differently than you’d expect. Try not to space them out like that when writing elseifs.

In any case though, if you want to learn about these functions then the first best place you should consult is documentation, it often holds the exact answer you need - refer to the above post for the documentation page. Ceiling and floor are names describing the type of rounding that’ll be performed (always up and always down respectively). Just feed a number into them as they’re functions.

math.ceil(MaxTimesec) -- Round up
math.floor(MaxTimesec) -- Round down
math.round(MaxTimesec) -- Round based on integral (decimal) part
2 Likes

I agree with @colbert2677 and @Administrat0r_J.

And also, What is the context behind this? Could you explain MaxTimesec?

The integral part of a number is its integer, not its decimal.

so you know im displaying the length of songs like in youtube.

like in the photo

and MaxTimeSec is the Seconds Of Length Of The Song
Which Here i use to convert miliseconds into seconds

Either math.ceil or math.floor. it doesnt really matter.

math.ceil = 0.1 → 1
math.floor = 0.1 → 0

I’d use math.floor in your case.

MaxTimesec = math.round(MaxTimesec)
1 Like

I’m not sure if this’d help you, but you can take a look. It’s a post I made (almost) a year ago (I was a disgusting scripter). You can take a look at it, and learn a few stuff about math

The post

You would use math.ceil if you wanted to round 45.7 to 46, as the name implies “ceiling” it rounds a number up unlike math.floor which rounds a number down. There is also math.round but I personally don’t use it much. Similar functionality though.