Limit the characters of a "newText"

@SGS

Really thanks. This is gold for me. I will study it thoroughly.  :) 

@dodi_games, the more syntax a language has, the more it can be terrifying.

string.format is really powerful and you don’t have to know all the options. But basically the first parameter to the function is the formatting string in this case:

“%0.3f”

The % says insert a value from the list of parameters following the format string. In this example there is only one % so it’s only going to take one value, which is the “valueToFormat” variable nor number.  After the % it specifies how to format that value. In this case, it says any number of numbers before the . including a 0 if it’s less than 1. And at max 3 decimal places after the .  The “f” says to expect a floating point number.  %5d would say to take a number, chop off all decimal places and output a 5 digit digit number. But %05d would lead it with 0’s.   It’s pretty cool actually.  In my game I use this to format my score since I want the score to always be 7 digits long:  

local scoreText = display.newText(string.format("%07d", myScore), display.actualContentWidth - 50, 25, native.systemFontBold, 16)

Here is another cool example.

local levelCompleteTitle = display.newText( string.format("Level %d Complete", currentLevel), display.contentCenterX, display.contentCenterY - 180, "carbon", 32 )

In this case instead of doing “Level " … currentLevel … " Complete” I can just use this and have it insert the number in the middle of the string.

Rob

Thanks a lot @Rob. Now I understood the use of string.format well.