Jump to content

Recommended Posts

I calculated a number and it displayed as something like,,  55.2333333333%.  I assumed all I'd need to do is math.round(2)(Equation) but that doesn't seem to be the case, at all..
These same two methods show up all over forums for how to round numbers, but it's like nothing I've seen before.  Does anyone know how this works?
This is what I'm trying to round down:  local percent = inst.components.finiteuses:GetUses() / 2400 * 100

http://lua-users.org/wiki/SimpleRound
 

function round(num, numDecimalPlaces)
  local mult = 10^(numDecimalPlaces or 0)
  return math.floor(num * mult + 0.5) / mult
end

 

https://steamcommunity.com/app/573090/discussions/0/1639792569839228614/

Edited by FurryEskimo
Link to comment
https://forums.kleientertainment.com/forums/topic/121470-rounding-numbers-in-lua/
Share on other sites

9 hours ago, FurryEskimo said:

I calculated a number and it displayed as something like,,  55.2333333333%.

Internally LUA floats follow IEEE-754's specification for floating point.

For displaying a float, I'd suggest using formatting parameters that you can get with string.format:

for i=0,2400,100
do
  local percent = i / 2400 * 100
  print(percent, string.format("%.0f", percent), math.floor(percent))
end

Output:

0.0              0    0
4.1666666666667  4    4
8.3333333333333  8    8
12.5             12   12
16.666666666667  17   16
20.833333333333  21   20
25.0             25   25
29.166666666667  29   29
33.333333333333  33   33
37.5             38   37
41.666666666667  42   41
45.833333333333  46   45
50.0             50   50
54.166666666667  54   54
58.333333333333  58   58
62.5             62   62
66.666666666667  67   66
70.833333333333  71   70
75.0             75   75
79.166666666667  79   79
83.333333333333  83   83
87.5             88   87
91.666666666667  92   91
95.833333333333  96   95
100.0            100  100

Note that the first column is default float printing, second is using rounding but not affecting the value used, and the last is rounding down always.

 

If you want just one extra decimal point you can use "%.1f" for the format parameter.  More on parameters for string.format:

https://www.lua.org/manual/5.3/manual.html#pdf-string.format

 

I'd advise to only use this for displaying purposes and for all logic to use the actual float without any rounding/truncation.  But there are cases where this behaviour is desired, so apply to your use case.

@CarlZalph
I followed, a bit of that..  I'm still trying to figure out how to input these values into the formula, and how to get the output..  My current guess is a little like this:

Input: inst.components.finiteuses:GetUses() and inst.components.finiteuses:GetMaxUses()

for i = inst.components.finiteuses:GetUses(), inst.components.finiteuses:GetMaxUses(), 100
do
  local percent = i / inst.components.finiteuses:GetMaxUses() * 100
  return(percent, string.format("%.2f", percent), math.floor(percent))
end

Output: percent

Currently I can insert your code directly and the game doesn't crash, but the value of 'percent' doesn't change.  My attempts to edit the code just make the server fail to start.

Oh, ok..  Well then how do I get a value of of this?  'Print' sends the value to the log file, right?  But I need it as a variable the characters say. (I'm updating the inspection dialogue of a tent with it's percent durability value.)

local percent = inst.components.finiteuses:GetUses() / inst.components.finiteuses:GetMaxUses() * 100 -- example: 15.1515151515
local roundedPercent = string.format("%.2f", percent) -- example: 15.15

Resource for variables: https://www.lua.org/pil/4.2.html

Edited by penguin0616
additional references
  • Like 1

Figured it out:

local percent = math.floor(inst.components.finiteuses:GetPercent() * 1000) / 10  --Rounds to one decimal place.

I believe I'm using an integer division, which is normally done using // but that just seems to make my mod break.

Edited by FurryEskimo
50 minutes ago, FurryEskimo said:

Figured it out:

local percent = math.floor(inst.components.finiteuses:GetPercent() * 1000) / 10  --Rounds to one decimal place.

I believe I'm using an integer division, which is normally done using // but that just seems to make my mod break.

LUA makes no distinction with numbers for DST as it's using a slightly modified version 5.1 base and as such all number types are stored as 64-bit doubles.

 

But yeah, I wasn't sure if you were asking for printing things out to the user or for internal use, the code I provided was just an example with some prints.

The thing you have made there will round down to the nearest 0.1 for the final product.  But for printing this in a string to the user anywhere, you'd want to still use the "%0.1f" formatted printing.

@CarlZalph
I probably would if I'd known how, haha.  I kept try and trying, but could figure out how to get a dynamic value(?) into and out of that formatting..

I'm going back and fixing up a bunch of old code, and now I'm on a periodic task.  Easy, simply, no big deal, except for some reason, stopping a periodic task caused my game to crash.
inst.task = inst:StopPeriodicTask(1, onfire)  --None of these work.
inst.task = inst:StopPeriodicTask(onfire)
inst:StopPeriodicTask(1, onfire)
inst:StopPeriodicTask(onfire)

Edit:  I tried using "inst:DoTaskInTime(1, onfire)" instead, but that also seems to cause it to crash..  It's weird..
Edit:  Got it.  Turns out I had the fire condition set to repeat this function, but it was also called when the tent was hammered (originally used to turn it off).  When the code was called twice, it would crash.  I Think,,, it's working now.  (Testing implies that it's not working, so again, here I go trying to figure it out.)

Edited by FurryEskimo

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...