I’m looking for a help with this. I want my “newText” to only show 7 characters because it is a result of a mathematical equation and when the result is a periodic number the newText leaves my content area preventing its reading on the screen of the phone.
Ex: 70.833333333
I want my text to show only 70.833
Here is the code.
-- calc button function local function calcButtonTouch( event ) if ( event.phase == "began" ) then audio.play( flick ) elseif ( event.phase == "ended" ) then local sum = tonumber( field\_1.text ) + tonumber( field\_2.text ) local mult = tonumber( field\_1.text ) \* tonumber( field\_2.text ) local step\_1 = sum / mult local step\_2 = step\_1 \* tonumber( field\_3.text ) \* tonumber( field\_4.text ) \* 6.1 local finalStep = step\_2 \* tonumber( field\_5.text ) result.text = finalStep end end
I try this
... elseif ( event.phase == "ended" ) then local sum = tonumber( field\_1.text ) + tonumber( field\_2.text ) local mult = tonumber( field\_1.text ) \* tonumber( field\_2.text ) local step\_1 = sum / mult local step\_2 = step\_1 \* tonumber( field\_3.text ) \* tonumber( field\_4.text ) \* 6.1 local finalStep = step\_2 \* tonumber( field\_5.text ) myNum = 7 -- max characters if string.len(result.text) \> myNum then result.text = result.text:sub( 1, 7 ) end result.text = finalStep end ...
Nothing happens, continue giving me the long result.
I have the “newText” created in the “scene create”, the “textFields” created in the “scene show” all the functions and the listeners are at the top where it says that the functions in the template scene go. In the “calcButton” function, after doing the math procedure, replaced the “finalStep” with the “result.text” and everything is fine. What I can not see is how to limit the number of characters that are displayed in the “result.text” on the screen not in the console. Try “string.len” but it does not give me result. “math.round” api only has a brief explanation and as examples only has “print statements” that I understand are only displayed in the console. I’m looking for how to solve this. This should be extremely easy for a good programmer, but for me being a graphic artist with little experience in programming is a rather difficult task. I am motivated with Corona and its new goals.
Why I need to write 2 times result.text = “finalStep”? One before “if statement” and another after the end of it. I do it and receive an error saying “bad argument #1 to ‘len’ (string expected, got table)”
You don’t need the second assignment only the first one and, in case the string is longer than 7 chars, the one with the :sub() call.
In the code snippet where you got the bad argument error you used result as a parameter to string.len instead of result.text. I can only guess what your code does as the few lines don’t include f.i. what kind of object result is.
My guess is that it’s a Corona text object which is of type table but string.len expects a string.
That said, if you still get the same error when you use result.text instead of result, then you probably should post more of your code as the problem is something else then.
I’m sure what I have is a string management problem.
I’m reading a lot but I still do not see it.
I’ll explain a little better.
-- Scene event functions Here are all the functions and listeners of all buttons and textFields -- create() all scene image objects, buttons, scrollViews -- show my textFields -- hide removeSelf() nil my textFields -- destroy
ok
first image is an example of my app, second image is the problem and third image is what I want
-- calc button function local function calcButtonTouch( event ) if ( event.phase == "began" ) then audio.play( flick ) elseif ( event.phase == "ended" ) then if field\_1.text == "" or field\_1.text == "value" or field\_2.text == "" or field\_2.text == "value" or field\_3.text == "" or field\_3.text == "value" or field\_4.text == "" or field\_4.text == "value" or field\_5.text == "" or field\_5.text == "value" then result.text = "0" elseif tonumber( field\_1.text ) and tonumber( field\_2.text ) and tonumber( field\_3.text ) and tonumber( field\_4.text ) and tonumber( field\_5.text ) then local sum = tonumber( field\_1.text ) + tonumber( field\_2.text ) local mult = tonumber( field\_1.text ) \* tonumber( field\_2.text ) local step\_1 = sum / mult local step\_2 = step\_1 \* tonumber( field\_3.text ) \* tonumber( field\_4.text ) \* 6.1 local finalStep = step\_2 \* tonumber( field\_5.text ) myNum = 7 -- max characters if string.len(result.text) \> myNum then result.text = result.text:sub( 1, 7 ) end result.text = finalStep end end return true end
Sorry for the first impression did not recognize the 3dp … this is very interesting, I will use the combination.
One question, can you tell me what “string library” should study to insert characters between my result? I would like if the result is more than 1000 can put a comma so 1,000.
I have a function that handles standard notation. May help you?
function comma\_value(amount,dontAddPlusSign) local suffix, formatted = "", 0 if dontAddPlusSign == nil then dontAddPlusSign = true end if amount == nil or amount == "" then amount = 0 else amount = tonumber(amount) end if mabs(amount) \> 1000000000000 then amount = amount / 1000000000000 suffix = "t" formatted = tonumber(string.format("%.1f", amount)) elseif mabs(amount) \> 1000000000 then amount = amount / 1000000000 suffix = "b" formatted = tonumber(string.format("%.1f", amount)) elseif mabs(amount) \> 1000000 then amount = amount / 1000000 suffix = "m" formatted = tonumber(string.format("%.1f", amount)) elseif mabs(amount) \> 100000 then amount = amount / 1000 suffix = "k" formatted = tonumber(string.format("%.1f", amount)) else formatted = mfloor(amount) end while true do formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2') if (k==0) then break end end if dontAddPlusSign then return formatted..suffix else if (formatted == "0" or formatted == "-0") and dontAddPlusSign then formatted = "" end if amount \> 0 then return "+"..formatted..suffix else return formatted..suffix end end end