Welcome, Guest. Please Login
Tinderbox
  News:
IMPORTANT MESSAGE! This forum has now been replaced by a new forum at http://forum.eastgate.com and no further posting or member registration is allowed. The forum is still accessible via read-only access for reference purposes. If you wish to discuss content here, please use the new forum. N.B. - posting in the new forum requires a fresh registration in the new forum (sorry - member data can't be ported).
  HomeHelpSearchLogin  
 
Pages: 1
Send Topic Print
Formatting a number with leading zeroes (Read 1786 times)
Jack Baty
Full Member
*
Offline



Posts: 96

Formatting a number with leading zeroes
Feb 16th, 2014, 4:35pm
 
I have a document with a numeric user attribute, Roll Number. I would like each note's DisplayExpression to generate strings like: "Roll-001", "Roll-013", etc.

I can pad with spaces using Number.format() but can't seem to figure out how to display a 1 as 001. Possible?
Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Formatting a number with leading zeroes
Reply #1 - Feb 16th, 2014, 6:09pm
 
Not really.  Her's a convoluted workaround. We'll assume the numbers needed range from 000 to 999 and are in $MyNumber. Also add a Number attribute $MyNumberA. In $PadString, store "000". add a String attribute $TitleString in which you'll store the fully formed 'number' lke 'Roll-003'. The latter ensures the calculation is done before use in $DisplayExpression (the latter context being less efficient for the task). The rule is then:

$MyNumA = 3-($MyNumber.format(0).size.toNumber);
$TitleString = "Roll-" +$PadString.substr(0,$MyNumberA)+ $MyNumber.format(0)


In convoluted first part takes $MyNumber and turns it into a string, so se can measure its length (size) before turning that string back into a number to subtract from 3. The latter result tells the next part how many padding zeroes to add using a variable length substring pulled from $PadString.

The $DisplayExpression is now:

$TitleString

…or whatever expanded string you want, including $TitleString.

I seem to recall suggesting Number.format() be allowed an optional third input which would be the padding character to use instead of the default space. However any such change, if taken up, won't occur in v5.x.
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Jack Baty
Full Member
*
Offline



Posts: 96

Re: Formatting a number with leading zeroes
Reply #2 - Feb 17th, 2014, 7:11am
 
Thank you, Mark. Your workaround should suit me fine. Thanks for the legwork!
Back to top
 
 
  IP Logged
Pages: 1
Send Topic Print