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 numbers with commas [solved: v5.10.3+] (Read 7559 times)
james a. foster
Full Member
*
Offline



Posts: 130

Formatting numbers with commas [solved: v5.10.3+]
Mar 23rd, 2012, 5:02pm
 
[Topic split by admin]

So, IS there a format specification to get the commas into a long number, e.g.  "5,000,000.00" rather than "5000000.00"?
Back to top
 
« Last Edit: Apr 12th, 2012, 7:23am by Mark Anderson »  
  IP Logged
Mark Bernstein
YaBB Administrator
*
Offline

designer of
Tinderbox

Posts: 2871
Eastgate Systems, Inc.
Re: Exporting a simple list...
Reply #1 - Mar 23rd, 2012, 5:08pm
 
Not at present.  (we've wandered a bit afield from exporting lists...new topic?)

Back to top
 
 
WWW   IP Logged
Paul Walters
Full Member
*
Offline



Posts: 267

Re: Formatting numbers with commas
Reply #2 - Mar 24th, 2012, 6:24pm
 
Quote:
So, IS there a format specification to get the commas into a long number, e.g.  "5,000,000.00" rather than "5,000,000.00"?

Must be the twilight and my failing eyesight - what's the difference between those two numbers?  Sad

I think it was 500,000 vs 500000-MB
Back to top
 
« Last Edit: Mar 24th, 2012, 10:19pm by Mark Bernstein »  
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Formatting numbers with commas
Reply #3 - Mar 24th, 2012, 7:45pm
 
I think this is under consideration (edge cases - not all locales use comma delimiters, etc). Meanwhile, for those comfortable with the command line (courtesy of here) try this at a command line:

printf "%'d\n" 12345678  --> 12,345,678

BUT:

printf "%'d\n" 12345678.123  --> printf: 12345678.123: invalid number

...plus I think the printf result is locale-dependent. This probably works in everyone's favour (i.e. the delimiter used will be for your locale if not a comma) except for those using a locale other than that whole normal format they expect.

In principle the following ought to work - though it doesn't compile as a $Rule.  The CL code is problematic as it uses both singe and double quotes:

$MyString = runCommand('printf "'+"%'"+'d\n" '+$MyNumber)

IIRC, this method of mixing TB's string enclosing quotes to 'wrap' opposing quote types works in other scenarios so I'm not quite sure why not here.  Whatever, I think the answer may be to wait for a built-in solution!

Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Formatting numbers with commas
Reply #4 - Mar 26th, 2012, 7:30am
 
Over the weekend, another approach occurred to me. This one is to set a String to the Number - which we're doing anyway - and then use a regex with .replace(). so:

$MyString = $MyNumber.replace("(\d)(?=(\d{3})+[^\d])","$1,")

12345.678 -> 12,345.678

In some locales, spaces are use instead of comma, in which case use "$1 " instead of "$1," as the replacement string:  

12345.678 -> 12 345.678

Notes:
  • There is an implicit Number -> String coercion here as .replace() doesn't chain to Number-type data.
  • Initially, I tried setting $MyString = $MyNum and only then applying .replace() to $MyString, but it seems OK to leave out that step.
  • If you have decimal places but whose number of places vary (i.e. you can't be certain how many) don't be tempted to use Number.precision(N) as if N is omitted, zero is assumed, all decimal places are omitted rounding out to an integer output.  IOW, Number.precision() for 4.95 is "5". Same applies for Number.format().
  • Edge case [sic] with long number sequences. Although exponential formats are allowed with Number-type data for very large or small number, TB doesn't seem to honour explicit digit strings of over 9 characters. The latter seem to get round/truncated to 9 characters, so although a Number-derived String of over 9 characters can be created likely many will just be zeroes. Most likely this is because to date there hasn't been a need for playing with really long number strings!


[later]

Digging further - having read this wikipedia page on numbering - let's assume we want space digit grouping and a comma as decimal separator:

$MyString = $MyNum.replace("(\d)(?=(?:\d{3})+([^\d]))","$1 ","x").replace("\.",",")

It uses 2 chained replacements, one for each delimiter:

1234567.89 --> 1 234 567,89

One replace is needed for each character type you wish to replace. So:

$MyString = $MyNum.replace("\.","'").replace("(\d)(?=(?:\d{3})+([^\d]))","$1.")

1234567.89 --> 1.234.567'89

Note in the latter the desired character changes mean the decimal point substitution needs to occur first.
Back to top
 
« Last Edit: Mar 26th, 2012, 10:26am by Mark Anderson »  

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Formatting numbers with commas
Reply #5 - Apr 12th, 2012, 7:21am
 
A simple solution for the comma-formatting is now built into TB v5.10.3+ via the number format "L". See notes on its use in format($NumAttr,"L") and $Number.format("L").

Note that the output string's format is dependent on the user's OS locale. See your OS System Preferences, Language & Text, Formats, Numbers to see how things will look in your current locale.
Back to top
 
 

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Pages: 1
Send Topic Print