Stormworks: Build and Rescue

Stormworks: Build and Rescue

Alistair Apr 13, 2020 @ 6:29am
How do I round numbers up on LUA script?
Its probably a bit stupid, I mean - in the editor theres probably a button saying "tO rOuNd uP nUmBeRs pReSs tHe bIg rEd bUtToN tHaT sAyS rOuNd uP" but I am making an LUA script right now that displays two things - time and speed. The numbers are a really long, seemingly endless decimal and i am using a 2 x 2 monitor. Does anyone know how to solve this? Thanks :D
< >
Showing 1-15 of 28 comments
Unit 744 Apr 13, 2020 @ 7:07am 
I know math.floor() will simply truncate. There is probably a better built-in function for rounding, but you could pretty easily write your own function that checks a decimal; if it is 0.5 or greater, math.floor()+1, otherwise just math.floor().

Using math.floor will remove all the decimals when displaying too. If you want to only display a set number of decimals (say, two digits after the decimal), you can do the following:

math.floor(x*100)/100

this will display two digits after the decimal. Replace 100 with the desired number of digits after the decimal (10 for 1 after, 1000 for 3 after, etc.). If it makes more sense, math.floor(x*1)/1 displays 0 digits after the decimal.

Hope this helps.
yungmono Apr 13, 2020 @ 7:12am 
For me I would write:


screen.drawText( x, y, 'Speed:'..string.format('%.0f', speed))


%.0f= no decimal place
%.1f= 1 decimal place
%.2f= 2 decimal place
etc
Hope this helps you
Alistair Apr 13, 2020 @ 8:05am 
Thanks Everyone :D I will take all this information to make an easy to use interface :steamhappy:
Ra-Ra-Rasputin Apr 13, 2020 @ 10:04am 
You can also do X = X - ( X % 1 ) to simply snip off the decimals completely if you're not interested in actual rounding.
Last edited by Ra-Ra-Rasputin; Apr 13, 2020 @ 10:04am
Alistair Apr 13, 2020 @ 10:23am 
I am finding it quite hard to understand this (yes I know I am stupid lol) . Here is my current script - if anyone could reply telling me what I need to add and where that would be helpful. I appreciate all the feedback :D


function onTick()
input.getNumber(1)
input.getNumber(2)
end

function onDraw()
w = screen.getWidth(32)
h = screen.getHeight(32)
screen.setColor(255, 0, 0) -- Red
screen.drawText(2, 2, "Time:")
screen.drawText(2, 10, "Speed:")
end

:D
Ra-Ra-Rasputin Apr 13, 2020 @ 11:51am 
Ceiling is just the opposite of floor though. It rounds 10.00000000000000001 to 11.
ElfBossHogg Apr 13, 2020 @ 12:02pm 
For rounding you can use the following...

math.floor(x+0.5)

If the 10th's part of number is less then 0.5 then floor will always put it to the "low". If it's over the adding 0.5 will roll it and floor will make it the "high"

For example... rounding 10.3 should be 10
math.floor(10.3+0.5)=math.floor(10.8) = 10

rounding 10.8.. should be 11
math.floor(10.8+0.5)=math.floor(11.3)=11

Now for rounding up...
SOLUTION: math.floor(x+1)

x+1 will always put you in to the "next set" which when floored will bring you to the start of the next set.. which is the round up.

If x=0.1 then round up = 1...
math.floor(0.1+1)=math.floor(1.1)=1

if x=1.9 round up would be 2...
math.floor(1.9+1)=math.floor(2.9)=2

if x=198.85 then round up = 199
math.floor(198.85+1) = math.floor(199.95)= 199

NOTE: This will only work for positive numbers.
Last edited by ElfBossHogg; Apr 13, 2020 @ 12:04pm
Ra-Ra-Rasputin Apr 13, 2020 @ 12:10pm 
ElfBossHogg hits in with the 200 IQ play. Yeah, that absolutely will work :lunar2019deadpanpig:
DanDanAttack Apr 13, 2020 @ 12:21pm 
Originally posted by Ra-Ra-Rasputin:
Ceiling is just the opposite of floor though. It rounds 10.00000000000000001 to 11.
Yep. But it’s simple enough for what this guy wants.
Jorg Hammond Apr 13, 2020 @ 12:29pm 
If one does not care about rounding the last decimal but just about the display, the easiest way appears to me to be the string format one which yungmono described.
Last edited by Jorg Hammond; Apr 13, 2020 @ 12:29pm
ElfBossHogg Apr 13, 2020 @ 12:31pm 
Originally posted by DanDanAttack:
Originally posted by Ra-Ra-Rasputin:
Ceiling is just the opposite of floor though. It rounds 10.00000000000000001 to 11.
Yep. But it’s simple enough for what this guy wants.

Yep... actually I think it may be exactly what he needs right out of the box.

math.floor(x+1) will give the exact same result as math.ceil(x). Math Ceiling is the rounds up which is what the op was asking for.
Alistair Apr 14, 2020 @ 1:14am 
OK I have one more stupid question. Where in my script should I put all these? :D
Ra-Ra-Rasputin Apr 14, 2020 @ 1:25am 
Where-ever you want the number to be rounded :)
You can either round the number directly on assignment, which means you only round it once (optimized), but this is really bad if you use the number to control something, like balance.

Your other option is to round it only when it needs to be rounded e.g. when you display it on screen.
Alistair Apr 14, 2020 @ 1:48am 
Originally posted by Ra-Ra-Rasputin:
Where-ever you want the number to be rounded :)
You can either round the number directly on assignment, which means you only round it once (optimized), but this is really bad if you use the number to control something, like balance.

Your other option is to round it only when it needs to be rounded e.g. when you display it on screen.
:D Thanks
< >
Showing 1-15 of 28 comments
Per page: 1530 50

Date Posted: Apr 13, 2020 @ 6:29am
Posts: 28