Stormworks: Build and Rescue

Stormworks: Build and Rescue

Shillelagh 1 AGO 2019 a las 6:52 p. m.
Decimal limits in Lua?
I have made a simple lua script to display a few stats in my helicopter, yet with fuel, when i give it a number not in increments of 10, i get some odd number like 20.4588888888888, could i limit this to 2 digits after the decimal point, so i get a number more like 20.45.

Thanks in advance,
-Ashes2007
< >
Mostrando 1-2 de 2 comentarios
SterlingHagoromo 27 AGO 2019 a las 9:34 a. m. 
If I understand this well you want to round a number to some number of decimals. You can use regular math for that or use the lua string functions.

Full example:
https://lua.flaffipony.rocks/?id=sAojeg4T4

function round(x, decimals) -- This should be less naive about multiplication and division if you are -- care about accuracy around edges like: numbers close to the higher -- values of a float or if you are rounding to large numbers of decimals. local n = 10^(decimals or 0) x = x * n if x >= 0 then x = math.floor(x + 0.5) else x = math.ceil(x - 0.5) end return x / n end function onTick() f = 1.2412165 -- Round using regular math.... trimmed = round(f, 2) -- Round using string libraries... trimmed2 = tonumber(string.format(".2f", f)) end

Keyojin 27 AGO 2019 a las 11:35 a. m. 
I use this. gN(#) is my input value that i want to display round up.

GPSx = math.floor(gN(5) * 100)/100
GPSy = math.floor(gN(6) * 100)/100
Última edición por Keyojin; 27 AGO 2019 a las 11:37 a. m.
< >
Mostrando 1-2 de 2 comentarios
Por página: 1530 50

Publicado el: 1 AGO 2019 a las 6:52 p. m.
Mensajes: 2