How to round numbers

I want to know how to round number since math.floor isn’t working. If I plug in 97 as the level.value I get a number like 3.634325413636 e+17
i want to display this on a text label but that number is just way too big.

local expNeeded = math.floor(15 * 1.5 ^ level.Value)

This may be a hackier method than whatever may be included with LuaU’s built-in math functions.

What you can do is check for any decimal points in a number (assuming there is only one), and then checking the first number to the right of that point. If the number is 4 or below, keep the number the same. If the number is 5 or more, add 1 to the overall number and remove everything right to the decimal point.

I don’t know exactly how this would be implemented, but it could potentially work.

You don’t need to round or floor the number for this. Your problem is that your number is enormous. Huge numbers get represented in scientific notation. You’ll need to manually construct the string you want.

Or just use a smaller number. x^97 is kind of absurd.

1 Like

How would I do that? Sorry, I’m still a beginner.

Actually, I think you might be using math.floor wrong.

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

If you ran this function while passing in a number as n, the number would be rounded to the nearest whole number. I’m not sure if this will work in your case, but it’s worth a try.

Here’s how you would most likely use it -

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

local expNeeded = round() -- insert number your trying to round

-- then you can do whatever you want with expNeeded afterward.

Not too long ago roblox added math.round()

You can literally just do math.round(number)

Hope this helped.

7 Likes

Are you sure? I cant seem to find any documentation for that in the API reference.

Yeah interestingly enough I can’t find any docs on it either. I happen to know from experience though.

I accidentally used math.round() in a project because I can use it in other languages, and it just worked.

Yeah, math.round() isn’t documented anywhere, but it still achieves the rounding.

Well if you really want a huge number you could store several smaller numbers and then patch them together to display the massive number. I believe this is what Cookie Clicker does.

local hundreds = 000
local thousands = 000
local millions = 000

function add(n)
   hundreds = hundreds + n
   -- carry over to thousands
   thousands = thousands + math.floor(hundreds/1000)
   hundreds = hundreds % 1000
   -- carry over to millions
   millions = millions + math.floor(thousands/1000)
   thousands = thousands % 1000
end

function toString()
   return millions .. ',' .. thousands .. ',' .. hundreds
end

@alimm123 This isn’t a rounding issue

@RepValor That’s pretty interesting.

Roblox uses LuaU, which is derived from Lua. The thing is that Lua isn’t derived from anything. I think it might be that Roblox using a C# VM and may have taken some functionality from C# and integrated it into LuaU.

This is also why you can use += even though it isn’t fully documented.

1 Like

No, that is not the case. The engineers behind Luau purposefully implemented some functions and operators specifically for Roblox. You can find a list of syntax differences here:

Specific functions should be documented on the wiki, but new ones take a bit to get added.

3 Likes

3.634325413636 e+17 would also be known as:
3.63432541363600000000000000000
e+17 is technically 17 more numbers, so they might not be 0’s. So I am guessing you wanna round this to like 3 or 3.6

That’s not how scientific notation works. I’d recommend checking this page out for a good explanation.

3 Likes

I was just making sure that I knew what he was saying

Yes, and I am trying to clarify what he was saying. I think you might’ve misread or something.

3 Likes

Also, thats different. Thats putting the power of. For instance, 5 to the power of 3 is 125 (5x5x5)
Or 3 to the power of 2 is 3x3
But 3e+2 would be 300(3 with 2 zeros)

I was talking about e+, not notation.

x e+y is just another way of typing x*10^y. That’s why I posted the link that explains it more in depth.

3 Likes

Thats not what I am saying. I am saying when the number said e+16 that means there is 16 more numbers, I was using 0’s as an example, but that number has 16 more numbers, it is just not showing it because it is so long.

What I am trying to say here is:
I was just saying that 3.634325413636 also has another 17 numbers (Not 16 I said that in the last thing sorry) and I was just using 17 zeros as an example.