Need Help With Score Keeping

My game’s score is actually money (i.e. $1.42) and I’m having trouble getting it to work the way I want it. I tried having dollars and cents variables, but the cents would only display one digit until it was 10 or more.

Can you guys think of a better way to do this?

P.S. I tried using three variables (one for each place value), but it was a little slow when updating because it had to check if each place value was 10 or more. [import]uid: 103624 topic_id: 19354 reply_id: 319354[/import]

http://lua-users.org/wiki/FormattingNumbers
look at this, maybe it will help you [import]uid: 16142 topic_id: 19354 reply_id: 74707[/import]

It says that this [lua]amount = 22333444.5634
print(format_num(amount,2,"$"))[/lua] translates to $22,333,444.56. I think I could use this. :smiley:

Do I need the format_num method in my code to use it? And, if I had my score value start at 0, wouldn’t it display without zeros for cents? Or would it add them? [import]uid: 103624 topic_id: 19354 reply_id: 74719[/import]

I would simply do:

  
 text = string.format("$%.02f", score)  
  

This should output the dollar sign, then any number of digits followed by two digits of decimal points. Of course this method won’t apply commas.
[import]uid: 19626 topic_id: 19354 reply_id: 74727[/import]

So then how would I add a score value to it? Could you write a quick example, please? [import]uid: 103624 topic_id: 19354 reply_id: 74780[/import]

Well lets assume you have a display text object for your score:

local score = 0  
local scoreText = display.newText(string.format("$.02f", score), 0, 0, "Helvetica", 20)  
scoreText.x = display.contentWidth / 2 -- centering, place it where you want it....  
scoreText.y = 15  
scoreText:setTextColor(255,255,255)  
  
....  
-- later on.....  
....  
  
score = score + amountEarned -- amountEarned is how much you want to add to the score.  
scoreText.text = string.format("$.02f", score)  
  

[import]uid: 19626 topic_id: 19354 reply_id: 74783[/import]

It doesn’t seem to be working. :confused:

It just constantly displays “$0.02f”.

[lua]textMoney = display.newText(string.format("$0.02f", money), 0, 20, native.systemFont, 75)[/lua] [import]uid: 103624 topic_id: 19354 reply_id: 74814[/import]

oops typeo;
$ %.02f

need that percent sign.
[import]uid: 19626 topic_id: 19354 reply_id: 74822[/import]

Thanks! [import]uid: 103624 topic_id: 19354 reply_id: 74823[/import]