how to make a % sign show up at the end of a number?

I have a formula that computs a number as a percent. So say for example it calculates 20.9. Is there a way to make this come out on the display as 20.9%?

On a side note, is there a way to have the formula give me an end value that ends in a zero? For example, instead of 12.9, I would prefer it to say 12.90. Is that possible?

Thanks in advance!! [import]uid: 35535 topic_id: 31359 reply_id: 331359[/import]

?Nate112
in my opinion, u have to make a function to do that
ex.

function formula(number) return number.."%" end [import]uid: 25057 topic_id: 31359 reply_id: 125375[/import]

If you are showing it as text, eg; local numText = display.newText(blah blah blah) you could then do;
[lua]numText.text = number…"%"[/lua]

Above answer is excellent too, just wanted to show a different example :wink: [import]uid: 52491 topic_id: 31359 reply_id: 125389[/import]

For the second part of your question, padding a number with a zero to make it two digits, you can use string.format like this:

print( string.format("%.2f", “12.9”) )

That will print 12.90

Go Google lua string.format and you’ll find some good info on it.

Jay
[import]uid: 9440 topic_id: 31359 reply_id: 125393[/import]

Adding on to Jay’s post:

print(string.format("%.2f%",yourValue)

will print 12.90% for you (if yourValue = 12.9)

string.format() uses %'s to indicate place holder’s in a string to replace with some value. You need to escape the % with a \ to get an actual percent sign.

[import]uid: 19626 topic_id: 31359 reply_id: 125446[/import]

Thanks very much everyone! Now that I’m able to add % signs to the value the user inputs into my textfields, it doesn’t let my formula work well. I used, lets say, tonumber(box1.text) + tonumber(box2.text). Is there something I can add to ignore the …% I added the the user’s text when doing the final calculation in the formula? Hopefully that makes sense… :slight_smile: [import]uid: 35535 topic_id: 31359 reply_id: 125462[/import]

?Nate112
in my opinion, u have to make a function to do that
ex.

function formula(number) return number.."%" end [import]uid: 25057 topic_id: 31359 reply_id: 125375[/import]

If you are showing it as text, eg; local numText = display.newText(blah blah blah) you could then do;
[lua]numText.text = number…"%"[/lua]

Above answer is excellent too, just wanted to show a different example :wink: [import]uid: 52491 topic_id: 31359 reply_id: 125389[/import]

For the second part of your question, padding a number with a zero to make it two digits, you can use string.format like this:

print( string.format("%.2f", “12.9”) )

That will print 12.90

Go Google lua string.format and you’ll find some good info on it.

Jay
[import]uid: 9440 topic_id: 31359 reply_id: 125393[/import]

Adding on to Jay’s post:

print(string.format("%.2f%",yourValue)

will print 12.90% for you (if yourValue = 12.9)

string.format() uses %'s to indicate place holder’s in a string to replace with some value. You need to escape the % with a \ to get an actual percent sign.

[import]uid: 19626 topic_id: 31359 reply_id: 125446[/import]

Thanks very much everyone! Now that I’m able to add % signs to the value the user inputs into my textfields, it doesn’t let my formula work well. I used, lets say, tonumber(box1.text) + tonumber(box2.text). Is there something I can add to ignore the …% I added the the user’s text when doing the final calculation in the formula? Hopefully that makes sense… :slight_smile: [import]uid: 35535 topic_id: 31359 reply_id: 125462[/import]