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

How to properly use math.huge?

Asked by
Cuvette 246 Moderation Voter
7 years ago

Hi, i'm working on a game and need my leaderstats to show a number over 2,147,483,648. I can't quite work out how to interpret it in an intvalue.

I'm already converting any number that goes up to 2.1B in my leaderstats using a function I made below. But trying to convert it into anything over 2.1B just ends up going into the minus. Is there any way to allow a number over 2.1B to show in an intvalue?

function ConvertShort(Filter_Num)
    local x = tostring(Filter_Num)
    if #x>=10 then
        local important = (#x-9)
        return x:sub(0,(important)).."."..(x:sub(#x-7,(#x-7))).."B+"
    elseif #x>= 7 then
        local important = (#x-6)
        return x:sub(0,(important)).."."..(x:sub(#x-5,(#x-5))).."M+"
    elseif #x>=4 then
        return x:sub(0,(#x-3)).."."..(x:sub(#x-2,(#x-2))).."K+"
    else
        return Filter_Num
    end
end     
    player.leaderstats.Cash.Value = ConvertShort(Cash)
    end
end)

1 answer

Log in to vote
0
Answered by 7 years ago

Lua (and by extension Roblox) doesn't have any native support for numbers larger than a 32 bit signed integer. However, this doesn't mean you don't have options. (For the sake of example, I'm assuming that you're working with money)

If you know the value won't ever go negative on its own, you can account for the rollover and parse the negative numbers as positive numbers. This gives you another bit to work with, which may be all you need. However, if you're filling up 2.1 billion, then adding another 2.1 billion to the mix may not be enough to help you.

You could also change the initial values so that 2 means 2 thousand dollars instead of 2 dollars, and handle anything more precise with floating-point numbers. This has the advantage of being relatively simple to implement, but you may occasionally drift up or down a few dollars due to the inaccuracies inherent in floating-point arithmetic. This is the best solution, as long as you don't care about fractions of a dollar.

if Filter_Num < 1 then --Less than 1000
    return tostring(Filter_Num * 1000) --Individual dollars
elseif Filter_Num < 1000 then --Less than a million
    return tostring(Filter_Num).." K+"
elseif Filter_Num < 1000000 then --Less than a billion
    return tostring(Filter_Num / 1000).." M+"
elseif Filter_Num < 1000000000 then --Less than a trillion
    return tostring(Filter_Num / 1000000).." B+"
else --Trillions
    return tostring(Filter_Num / 1000000000).." T+"
end

The above example would return the string representation of Filter_Num (less any digit cutoffs, to keep things simple and readable). You would still have the looming threat of integer rollover, but it would only happen north of 2 trillion (instead of 2 billion). If you were truly desperate for trillions, you could jump from thousands to millions as the default denomination and have the low quadrillions to play with (at the major expense of accuracy at low quantities)

If you're truly fantastic at binary math, you can store the number in a string as binary (or hexadecimal to save space) and write your own arithmetic library to calculate addition, subtraction, and multiplication without using numbers. That way, you can have numbers of virtually arbitrary length without any risk of floating-point errors. This won't be particularly fast, however, and can be a pain in the butt to program for since you have to convert values to binary for use (not to mention the pain of getting the code written in the first place).

Finally, you could just alter your game so that it doesn't need to use the absurdly high quantities of money, and let integer rollover be a punishment for overzealous hackers. :P

Ad

Answer this question