Pad a number?

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
busterbluth
Cave Troll
Posts: 74
Joined: Sat Aug 17, 2019 3:49 pm

Pad a number?

Post by busterbluth »

Any idea how to pad a single digit number embedded in a string with leading zeroes if you want to see a 2 digit value?

I'm outputting a date value in this format: mm/dd/yyyy and I'd like to see

09/05/1859

as opposed to the default which is

9/5/1859

Thanks in advance!!

taustinoc
Dragon
Posts: 528
Joined: Mon Aug 03, 2015 6:30 pm

Re: Pad a number?

Post by taustinoc »

If you've got month, day and year as separate variables, I know it could be done with strformat(). I'm not aware of any functions that deal specifically with dates, but they may exist.

https://wiki.rptools.info/index.php/strformat

User avatar
aliasmask
RPTools Team
Posts: 9135
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Pad a number?

Post by aliasmask »

I don't think the wiki makes it very intuitive on how to use, so here's an example.

Code: Select all

[H: date = substring(json.get(getInfo("client"),"timeDate"),0,10)]
[H: year = listGet(date,0,"-")]
[H: month = listGet(date,1,"-")]
[H: day = listGet(date,2,"-")]

<!-- no leading 0s, direct variable name method -->
[R: strformat("%{month}-%{day}-%{year}")]<br>

<!-- leading 0s, substitution method -->
[R: strformat("%02d/%02d/%s",month,day,year)]

busterbluth
Cave Troll
Posts: 74
Joined: Sat Aug 17, 2019 3:49 pm

Re: Pad a number?

Post by busterbluth »

Thanks very much !!

Post Reply

Return to “Macros”