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: 299
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 »

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
Lemma-Class Veteran
Posts: 2404
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: How to show thousands separator for variable?

#2 Post by Ocelot »

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: 299
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 »

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: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: How to show thousands separator for variable?

#4 Post by Imperf3kt »

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.

Current project: GGD Mentor

Twitter

User avatar
meyaoi
Regular
Posts: 61
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 »

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 »

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: 1628
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 »

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 »

That did the trick. Thanks Remix!

Post Reply

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot], piinkpuddiin, snotwurm