Jump to content
  • 0

Can anyone tell me where's my mistake?


FangYX

Question

Quote

function SecConvert(seconds)
  if string.find(seconds, "%.") ~= nil then
    decimals = tostring(seconds):match("%.(.*)");
    if string.find(decimals, "%d%d%d%d") ~= nil then
      if tonumber(tostring(decimals):match("%d%d%d(%d)")) >= 5 then
        decimals = tonumber(tostring(decimals):match("%d%d%d"))+1;
      else
        decimals = decimals:match("%d%d%d");
      end
    else
      decimals = (decimals.."00"):match("%d%d%d");
    end
  else
    decimals = 0;
  end
  decimals = tonumber(decimals);
  seconds = tonumber(seconds);
  if seconds <= 0 then
    return "00:00:00,000";
  else
    hours = math.floor(seconds/3600);
    mins = math.floor(seconds/60 - (hours*60));
    secs = math.floor(seconds - hours*3600 - mins*60);
    return string.format("%02d:%02d:%02d,%03d", hours, mins, secs, decimals)
  end
end

print(SecConvert(120.05).."\n"..SecConvert(120.05).."\n"..SecConvert(120.05).."\n"..SecConvert(120.05).."\n"..SecConvert(120.05).."\n"..SecConvert(120.05))

And the output is

Quote

Script ended:

00:02:00,050

00:02:00,500

00:02:00,500

00:02:00,500

00:02:00,500

00:02:00,500

How to make it constantly ended with 00:02:00,050?

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

Interesting encounter, it seems to be a bug in GG's Lua implementation, because running the code with official implementation (whether of version 5.2 or 5.3) results in expected output.

The issue occurs in line 3: 

decimals = tostring(seconds):match("%.(.*)");

At the first invocation of the function, "decimals" global variable contains value of nil before the assignment, the expression at the right side of the assignment evaluates to "05" and value of "decimals" global variable is set to "05".

At the second and all next invocations of the function, "decimal" global variable contains numeric value 50 (at the second invocation) or 500 (from third invocation onwards) before the assignment, the expression at the right side of the assignment evaluates to "05", but value of "decimals" global variable is set to 5 (number type). Such behavior may be related with variable containing value of number type before the assignment, it seems that "05" is mistakenly coerced to number.

As a workaround, to not encounter the bug, you can just set value of "decimals" global variable to nil at the start of the function: 

decimals = nil

Even better idea is to make "decimals" local, since there is no need to access it outside the function. Add definition of the variable at the start of the function: 

local decimals = nil

Interestingly, the bug doesn't seem to occur with local variable, so you can also safely initialize the variable with default value of number type: 

local decimals = 0
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.