Jump to content

Commatizing numbers: Difference between revisions

J
m (minor corrections)
(J)
Line 78:
* The Wiki entry:   [http://en.wikipedia.org/wiki/Eddington_number Arthur Eddington's number of protons in the universe].
 
 
=={{header|J}}==
 
These rules are relatively baroque, which demands long names and minimally complex statements, thus:
 
<lang J>require'regex'
commatize=:3 :0"1 L:1 0
(i.0) commatize y
:
NB. deal with all those rules about options
opts=. boxopen x
char=. (#~ ' '&=@{.@(0&#)@>) opts
num=. ;opts-.char
delim=. 0 {:: char,<','
'begin period'=. _1 0+2{.num,(#num)}.1 3
NB. initialize
prefix=. begin {.y
text=. begin }. y
NB. process
'start len'=. ,'[1-9][0-9]*' rxmatch text
if.0=len do. y return. end.
number=. (start,:len) [;.0 text
numb=. (>:period|<:#number){.number
fixed=. numb,;delim&,each (-period)<\ (#numb)}.number
prefix,(start{.text),fixed,(start+len)}.text
)</lang>
 
In use, this might look like:
 
<lang J> (5;5;' ') commatize 'pi=3.14159265358979323846264338327950288419716939937510582097494459231'
pi=3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59231
'.' commatize 'The author has two Z$100000000000000 Zimbabwe notes (100 trillion).'
The author has two Z$100.000.000.000.000 Zimbabwe notes (100 trillion).
commatize '-in Aus$+1411.8millions'
-in Aus$+1,411.8millions
commatize '===US$0017440 millions=== (in 2000 dollars)'
===US$0017,440 millions=== (in 2000 dollars)
commatize '123.e8000 is pretty big.'
123.e8000 is pretty big.
commatize 'The land area of the earth is 57268900(29% of the surface) square miles.'
The land area of the earth is 57,268,900(29% of the surface) square miles.
commatize 'Ain''t no numbers in this here words, nohow, no way, Jose.'
Ain't no numbers in this here words, nohow, no way, Jose.
commatize 'James was never known as 0000000007'
James was never known as 0000000007
commatize 'Arthur Eddington wrote: I believe there are 15747724136275002577605653961181555468044717914527116709366231425076185631031296 protons in the universe.'
Arthur Eddington wrote: I believe there are 15,747,724,136,275,002,577,605,653,961,181,555,468,044,717,914,527,116,709,366,231,425,076,185,631,031,296 protons in the universe.
commatize ' $-140000±100 millions.'
$-140,000±100 millions.
commatize '6/9/1946 was a good year for some.'
6/9/1946 was a good year for some.</lang>
 
=={{header|REXX}}==
6,962

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.