Install Steam
login
|
language
简体中文 (Simplified Chinese)
繁體中文 (Traditional Chinese)
日本語 (Japanese)
한국어 (Korean)
ไทย (Thai)
Български (Bulgarian)
Čeština (Czech)
Dansk (Danish)
Deutsch (German)
Español - España (Spanish - Spain)
Español - Latinoamérica (Spanish - Latin America)
Ελληνικά (Greek)
Français (French)
Italiano (Italian)
Bahasa Indonesia (Indonesian)
Magyar (Hungarian)
Nederlands (Dutch)
Norsk (Norwegian)
Polski (Polish)
Português (Portuguese - Portugal)
Português - Brasil (Portuguese - Brazil)
Română (Romanian)
Русский (Russian)
Suomi (Finnish)
Svenska (Swedish)
Türkçe (Turkish)
Tiếng Việt (Vietnamese)
Українська (Ukrainian)
Report a translation problem
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.
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
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
https://pgl.yoyo.org/luai/i/math.ceil
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.
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.
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.