Jump to content




Help with rounding?


7 replies to this topic

#1 ZagKalidor

  • Members
  • 111 posts

Posted 08 June 2013 - 08:22 AM

Tach ihr german people,

es heißt ja hier Tutorial for beginner, also könnte man ja hier mal eine Frage stellen, ohne das man gleich gelächter erntet oder als noob beschimpt wird.

Wie definiere ich eine Variable um, um aus vielen Nachkommastellen eine ganze Zahl oder eine Zahl mit vordefinierter Anzahl an Nachkommastellen zu machen? Gehört eigentlich zu den ersten Basics aber ich finde einfach nirgendwo eine passable Antwort.

#2 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 08 June 2013 - 10:43 AM

View PostZagKalidor, on 08 June 2013 - 08:22 AM, said:

Tach ihr german people,

es heißt ja hier Tutorial for beginner, also könnte man ja hier mal eine Frage stellen, ohne das man gleich gelächter erntet oder als noob beschimpt wird.

Wie definiere ich eine Variable um, um aus vielen Nachkommastellen eine ganze Zahl oder eine Zahl mit vordefinierter Anzahl an Nachkommastellen zu machen? Gehört eigentlich zu den ersten Basics aber ich finde einfach nirgendwo eine passable Antwort.
math.floor() to yound it don
math.ceil() to round it up

#3 ZagKalidor

  • Members
  • 111 posts

Posted 08 June 2013 - 06:54 PM

Aaaaaah very vielen Dank bro,

Das bringt meine OCS-Kistenfüllstandsanzeige jetzt wirklich mal auf ein ansehlichses Format. Manchmal sieht man den Wald vor lauter Bäumen nicht.

Ich muss sagen, ich hatte ja diese mathematischen Funktionen im Lua-Manual schon mal überflogen, da ich die Antwort auch da vermutet hatte, bin aber trotzdem nicht drauf gekommen. Es ist halt für einen Anfänger auch nicht immer ganz so einfach zu verstehen. Me so noobish. Obwohl wirklich seltsam ist: Ich bekomme es hin mir eine Minimap von Daten des Proximity-Sensors zu programmieren aber schaffe es nicht eine Integer anzuzeigen. Schon komisch.

Jetzt habe ich das ganze ding nochmal durchgekaut und finde wieder keine Antwort auf die nächste Frage: Wie würde es lauten, wenn ich 2 Nachkommastellen haben will?
Sorry, das ich Euch mit so einfachem Mist belästige aber ich peil es einfach nicht.

Greez

#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 09 June 2013 - 03:59 AM

I've split this from Tutorials.

It is preferable to use English for your post, or at least copy/paste a Google Translate translation and add it to your post. Especially since, if you want help with OCS, the number of people who are able to effectively help with it is pretty small.

#5 ZagKalidor

  • Members
  • 111 posts

Posted 09 June 2013 - 04:53 AM

Sorry for that,

thought this thread was done, especialy for germans,

the main question was, how do i call a variable when it schould have 2 decimal digits. I learned before that it can be rounded to integer by math.floor - ceil but did not find a proper explanation for 2 digtis behind the comma.

Thx friends...

#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 09 June 2013 - 05:11 AM

Multiply by 100, round, then divide by 100.

#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 09 June 2013 - 06:20 AM

Also to do an actual round (i.e. 0.1—0.4 rounds down, 0.5—0.9 rounds up ), instead of just a round up/down, we do a little something like this.
local function round( num )
  return math.floor( num + 0.5 )
end

We could further extend this to allow us to specify how many decimal places we want
local function round( num, places )
  places = 10 ^ places
  return math.floor((num * places) + 0.5 ) / places
end


#8 ZagKalidor

  • Members
  • 111 posts

Posted 09 June 2013 - 10:33 AM

thx very much





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users