How to show thousands separator for variable?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
sapiboonggames
Veteran
Posts: 298
Joined: Thu Jan 05, 2012 8:53 am
Completed: I Love You, Brother [BxB]
Contact:

How to show thousands separator for variable?

#1 Post by sapiboonggames » Sun Apr 23, 2017 2:51 am

Hello guys, I'm back with another question :D
As the subject says, do you know how to show thousands separator for variable?
Let's say:

Code: Select all

$ money += 10000
Right now, if I show money by using [money], it's displayed as "10000", is there any way to display it as "10,000" or "10.000"?

Thanks! :D

User avatar
Ocelot
Miko-Class Veteran
Posts: 849
Joined: Tue Aug 23, 2016 10:35 am
Skype: miinipaa
Contact:

Re: How to show thousands separator for variable?

#2 Post by Ocelot » Sun Apr 23, 2017 3:29 am

The obvious way: transform number to string, insert separator as needed and print that string instead of raw number. You can easily do that using format: $ money_string = '{:,}'.format(money)

Correct way would be to define your class to hold money, with proper methods to add and substract money with invariant checking and override __repr__() magic method, so your class would be properly displayed whenever you need it with actual values.
< < insert Rick Cook quote here > >

User avatar
sapiboonggames
Veteran
Posts: 298
Joined: Thu Jan 05, 2012 8:53 am
Completed: I Love You, Brother [BxB]
Contact:

Re: How to show thousands separator for variable?

#3 Post by sapiboonggames » Sun Apr 23, 2017 3:39 am

Ocelot wrote:The obvious way: transform number to string, insert separator as needed and print that string instead of raw number. You can easily do that using format: $ money_string = '{:,}'.format(money)

Correct way would be to define your class to hold money, with proper methods to add and substract money with invariant checking and override __repr__() magic method, so your class would be properly displayed whenever you need it with actual values.
Thanks!
I used the transform number to string method, and it works great.
Visit my website: http://www.sapiboong.com

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3134
Joined: Mon Dec 14, 2015 5:05 am
Location: Your monitor
Contact:

Re: How to show thousands separator for variable?

#4 Post by Imperf3kt » Sun Apr 23, 2017 3:52 am

I looked at the explanation and scratched my head. I guess I don't even understand a lot of python yet.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Twitter
Imperf3kt Blackjack - a WIP blackjack game for Android made using Ren'Py
Free Android GUI - Updated occasionally

User avatar
meyaoi
Regular
Posts: 60
Joined: Thu Dec 03, 2015 6:07 am
Tumblr: meyaoigames
Deviantart: meyaoi
Contact:

Re: How to show thousands separator for variable?

#5 Post by meyaoi » Sun Apr 23, 2017 3:54 am

Imperf3kt wrote:I looked at the explanation and scratched my head. I guess I don't even understand a lot of python yet.
I don't really understand the 2nd method too :lol:
But the 1st method is simple enough.
You just need to display money_string variable after transforming it instead of the previous money variable.

HammeredEnt
Regular
Posts: 33
Joined: Sat May 04, 2019 8:09 am
Contact:

Re: How to show thousands separator for variable?

#6 Post by HammeredEnt » Thu Jun 11, 2020 3:56 am

Hi team,

I’m trying to get this to work myself but I’m clearly doing something wrong. Can someone spot-check my work here?

My variable is named 'jackpotfunds'. Prior to the start label of the script I’ve got:

Code: Select all

default jackpotfunds = 1000
$ money = '{:,}'.format(jackpotfunds)
In the game itself, I’m using the following to display that variable with the comma separator :

Code: Select all

"You now have [money]"

but it's literally coming up:

"You now have [money]"

rather than displaying the formatted string.

I primarily want to use this in an inventory screen (where the coding may be different) but I figure if I need to get that working in regular dialogue first and then hopefully it won't be different for the other screen.

Help?

User avatar
Remix
Eileen-Class Veteran
Posts: 1506
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: How to show thousands separator for variable?

#7 Post by Remix » Thu Jun 11, 2020 7:19 am

Your

Code: Select all

$ money = '{:,}'.format(jackpotfunds)
line is only running once and the point where you have the output is either before that or through a save that did not include that variable

As [] interpolation can use pyFormat, just use:

Code: Select all

"[jackpotfunds:,]"
Frameworks & Scriptlets:

HammeredEnt
Regular
Posts: 33
Joined: Sat May 04, 2019 8:09 am
Contact:

Re: How to show thousands separator for variable?

#8 Post by HammeredEnt » Thu Jun 11, 2020 9:13 am

That did the trick. Thanks Remix!

Post Reply

Who is online

Users browsing this forum: Captain Monocle