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

Converting an NumberValue to IntValue?

Asked by 5 years ago

The Title says everything, but i'll explain what is my objective with this.. Example:

local var1 = 1.87
local var2 = 1.16
local var3 = 3

--(somehow, the NumberValues converted into IntValues!)
print(var1,var2,var3)

OUTPUT

2 1 3

i don't know if there is a built in function for this.. tell me plz

0
this doesn't make any sense, is there something else you're doing? megukoo 877 — 5y

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
5 years ago

I think what you're trying to do is round a number to the nearest whole number.

Lua doesn't have this concept exactly built in, but it does have a very similar function. math.floor(x) is the biggest whole number under x.

For example, math.floor(1.1) is 1 and math.floor(2.9) is 2.

math.floor does what you want when the fractional part of a number is between 0 and 1/2, but it goes too low when it's between 1/2 and 1.

To fix, that we use math.floor(x + 0.5):

math.floor(1.1 + 0.5) --> 1
math.floor(1.4 + 0.5) --> 1
math.floor(1.5 + 0.5) --> 2
math.floor(1.6 + 0.5) --> 2
math.floor(2.2 + 0.5) --> 2
0
Thanks c: FullMetalEdward45221 106 — 5y
Ad

Answer this question