Number formatting

83 views
Skip to first unread message

taale...@gmail.com

unread,
Dec 10, 2020, 7:47:44 PM12/10/20
to TiddlyWiki
I haven't found anything that quite addresses what I'm looking for. Specifically, I'm looking for something that works like Excel's format function, so that I can specify that the number 12 appears as 00012 (= excel's "00000"), for example. Is there anything that does that already?











Saq Imtiaz

unread,
Dec 10, 2020, 8:05:04 PM12/10/20
to TiddlyWiki
There is a pad[] filter operator in the very soon to be released TW 5.1.23

Eric Shulman

unread,
Dec 10, 2020, 8:19:09 PM12/10/20
to TiddlyWiki
On Thursday, December 10, 2020 at 5:05:04 PM UTC-8 saq.i...@gmail.com wrote:
There is a pad[] filter operator in the very soon to be released TW 5.1.23

prior to TW5.1.23, padding can be done using split[] and join[], like this:

<$vars num="12">
{{{ [<num>addprefix[00000]split[]last[5]join[]] }}}
<$vars>

The resulting output is "00012".  Note that if the num value has more than 5 digits, it will be truncated to only show the last 5 digits.

-e

taale...@gmail.com

unread,
Dec 12, 2020, 9:30:58 PM12/12/20
to TiddlyWiki

taale...@gmail.com

unread,
Dec 12, 2020, 10:40:44 PM12/12/20
to TiddlyWiki
Well, that's not quite the solution after all, because I keep getting this error:

Uncaught RangeError: Maximum call stack size exceeded 

I don't have issues with the macro on tiddlywiki.com, so I don't know what I'm doing wrong. Can anyone figure out what's wrong?

I'm using the macro in the caption field like this:
<$macrocall $name="padder" str={{!!cnum}} len=2/>: {{!!title}}

The idea is to have the title replaced in link-lists with the caption, which shows the card number (cnum) and the title of the card, ala "05 : Cilantro" or "08 : Emerald".

And just in case, this is the macro definition I used (from that link I posted before, which is Mark S's code:

\define padder-reg() ^$(len)$$ 
\define padder(str,len,chr:"0") 
<$vars len=<<__len__>>> 
<$list filter="[<__str__>addprefix<__chr__>]" variable="padstr">
<$list filter="[<padstr>length[]!regexp<padder-reg>]" emptyMessage=<<padstr >>>
<$macrocall $name="padder" str=<<padstr>> len=<<__len__>> chr=<<__chr__>>/> 
</$list> 
</$list>
</$vars>
\end  

Eric Shulman

unread,
Dec 12, 2020, 11:41:33 PM12/12/20
to TiddlyWiki
On Saturday, December 12, 2020 at 7:40:44 PM UTC-8 taale...@gmail.com wrote:
Uncaught RangeError: Maximum call stack size exceeded 

This error is indicative of a run-away recursion.  Without digging into the code myself, I'm not sure why this is the case.  In any event, I suggest entirely avoiding the complexity (and processing overhead) of recursion by using the following alternative macro definition:
\define padder(str,len) {{{ [[$str$]addprefix[0000000000]split[]last[$len$]join[]] }}}

This will work to pad numbers up to 10 digits long (as determined by the number of zeros in the addprefix[...] operator) and you would use it in exactly the same way as the recursive macro:
<$macrocall $name="padder" str={{!!cnum}} len="2" />

Note that, starting with the soon to be released TW v5.1.23, there is a new filter operator, pad[...], which will accomplish the same result without needing any custom macro definition.
To use this new filter, you would just write the following inline filter instead of using <$macrocall ...>
{{{ [{!!cnum}pad[2]] }}}

enjoy,
-e

Aidan Grey

unread,
Dec 13, 2020, 12:38:46 AM12/13/20
to TiddlyWiki
Thanks Eric! That works - except not in the caption field. Still get the recursion error. I think waiting until v23 is out will have to do. 

Do we have an ETA yet?

--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/e7f56c27-bb5e-4045-a57a-220f87949a16n%40googlegroups.com.

Eric Shulman

unread,
Dec 13, 2020, 1:23:22 AM12/13/20
to TiddlyWiki
On Saturday, December 12, 2020 at 9:38:46 PM UTC-8 taale...@gmail.com wrote:
Thanks Eric! That works - except not in the caption field. Still get the recursion error. I think waiting until v23 is out will have to do. 
Do we have an ETA yet?

"very soon"... in the meantime, you can upgrade to use the pre-release 5.1.23, here:

Then, once 5.1.23 is official, you can upgrade again, to get any last-minute fixes, using:

-e
Reply all
Reply to author
Forward
0 new messages