Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I get rid of this part of an int?

Asked by 4 years ago
Edited 4 years ago

Alright, so, basically, I made a script so when you die, it divides your money by half, however, this method brings a bad result, which is, let me actually provide an example since I'll probably confuse all of you.

1.2< The arrow marks the part that I want to get rid of, can anyone tell me how to do that? I thought about using string.Sub() but I have no idea how to use it after all, anyways, I would appreciate if anyone would've been able to teach me this.

Thanks!

0
math.floor(1.2) jamespringles 105 — 4y

2 answers

Log in to vote
1
Answered by
Raccoonyz 1092 Donator Moderation Voter
4 years ago
Edited 4 years ago

There's a neat trick you can do with math.floor(). Below is an example.

local number = 1.2
local rounded = math.floor(number + 0.5)

This will round the value to the nearest whole number from which I assume you are trying to do. It works because math.floor() always returns the number without the decimal. You then add 0.5 so that if the number is above 0.5, it'll get rounded above.

Just want the decimal to be removed?

local number = 1.2
local rounded = math.floor(number)

This should work. If it doesn't, leave a comment. If it does, make sure to mark this question as the accepted answer.

You can read more about math.floor() here.

0
great answer User#23252 26 — 4y
0
Thank you. Raccoonyz 1092 — 4y
0
Um, it works quite all right until 1000, when it removes the three zeros, making it one, AddisonAvery 43 — 4y
0
I wasn't able to replicate that. Are you sure it's not working? Raccoonyz 1092 — 4y
View all comments (2 more)
0
Okay, erm, make a text label, and an int value. Set the text label to display the value of the int value, and make the int value 1000. Then, perform math.floor() on the int value's value. You should see what happens AddisonAvery 43 — 4y
0
I tried printing it. Couldn't replicate it. Raccoonyz 1092 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
local n1 = 1.2
local n2 = 1.6


local function round(n)
    return math.floor(n + 0.5)
end

print(round(n1))
print(round(n2))

this will print 1 2

Answer this question