Página 1 de 1

Convert Sting

Publicado: 09 Nov 2013 00:54
por milano88
Hi Everyone,
How to convert a sting 01:20 to normal string 10-20-30-90-100...
string = "01:20" --- convert to 80
format = string.format("%02.f", Math.Floor(string)));

pls help me :(

Publicado: 09 Nov 2013 16:55
por Metafunken
Do you mean convert "hours into integers" or something like that?

If this is the case, I made a function in almost pure lua code for a few weeks.

Here you go:
function HoursToInteger(nH)
	t = {};
	if (#nH >= 1) then
		n = 0;
		for k, v in string.gmatch(nH, "*-(%d+)") do
			n = n + 1;
			t[n] = k;
		end
		if (t ~= nil and table.maxn(t) == 3) then
			nHours = t[1] * 60;
			nMins = t[2] * 60 / 60;
			if (tonumber(t[3]) > 30) then
				nSecs = Math.Round(t[3] * 60 / 3600, 0);
			else
				nSecs = 0;
			end
		elseif (t ~= nil and table.maxn(t) == 2) then
			nHours = t[1] * 60;
			nMins = t[2] * 60 / 60;
			nSecs = 0;
		elseif (t ~= nil and table.maxn(t) == 1) then
			nHours = t[1] * 60;
			nMins = 0;
			nSecs = 0;
		else
			nHours = 0;
			nMins = 0;
			nSecs = 0;
		end
		return (nHours + nMins + nSecs);
	end
end
How do it works?

Put this function in the Global Functions code area, to call it just put, for example, in a button:
Input.SetText("Input2", HoursToInteger(Input.GetText("Input1")));
Of course you'll need two inputs, "input and output".

If you know something about lua then you'll know this code can be improved a lot.

Did this helps to you? Then we wait for your examples. Otherwise...

Regards.

By the way... I wait for your feedback.

Publicado: 09 Nov 2013 17:06
por milano88
works perfect Metafunken big thans :num1:

Publicado: 09 Nov 2013 17:22
por Metafunken
You're welcome, glad to help you. ;)

Publicado: 09 Nov 2013 17:27
por Metafunken
I forgot, this function can find all the delimiters within the string, including blanks. try it.

Publicado: 09 Nov 2013 17:52
por milano88
ohhhh awesome Metafunken :pc: tested 01:20 and 01 20 and 01.20 works perfect :num1: can be toz way 0120 or 0230 = 01:20 and 02:30

Publicado: 09 Nov 2013 18:37
por Metafunken
Not awesome, just analize the regex implemented in the string.gmatch() function, but thanks.

If you want 0120 as a result, then you must change the "return" with a, for example, "string.format()" function or simply just concatenate it, don't forget the zero in case of concatenate them.

As for the other, if you want to input 0120 and get the result as 01:20, must "reverse" my function, but in this case you must "insert" the delimiter.

If you have any doubt just post your code to get more help, if not me, maybe another one can helps you.

We need to participate!

Regards.

Publicado: 09 Nov 2013 19:21
por milano88
thanks for the help Metafunken ;)