Commatizing numbers: Difference between revisions

Content added Content deleted
(→‎{{header|Nim}}: fix case when `sep` has more than one character (e.g. `echo commatize("572561234.1D-4", period = 4, sep = "__")`))
(Added 11l)
Line 81: Line 81:
;Also see:
;Also see:
* The Wiki entry: &nbsp; [http://en.wikipedia.org/wiki/Eddington_number (sir) Arthur Eddington's number of protons in the universe]. <br><br>
* The Wiki entry: &nbsp; [http://en.wikipedia.org/wiki/Eddington_number (sir) Arthur Eddington's number of protons in the universe]. <br><br>

=={{header|11l}}==
{{trans|Nim}}

<lang 11l>F commatize(s, period = 3, sep = ‘,’)
V m = re:‘(\.[0-9]+|[1-9]([0-9]+)?(\.[0-9]+)?)’.search(s)
I !m
R s

V match = m.group()
V splits = match.split(‘.’)

V ip = splits[0]
I ip.len > period
V inserted = 0
L(i) ((ip.len - 1) % period + 1 .< ip.len).step(period)
ip = ip[0 .< i + inserted]‘’sep‘’ip[i + inserted ..]
inserted += sep.len

I splits.len > 1
V dp = splits[1]
I dp.len > period
L(i) ((dp.len - 1) I/ period * period .< period - 1).step(-period)
dp = dp[0 .< i]‘’sep‘’dp[i..]
ip ‘’= ‘.’dp

R s[0 .< m.start()]‘’ip‘’s[m.end()..]

V tests = [‘123456789.123456789’,
‘.123456789’,
‘57256.1D-4’,
‘pi=3.14159265358979323846264338327950288419716939937510582097494459231’,
‘The author has two Z$100000000000000 Zimbabwe notes (100 trillion).’,
‘-in Aus$+1411.8millions’,
‘===US$0017440 millions=== (in 2000 dollars)’,
‘123.e8000 is pretty big.’,
‘The land area of the earth is 57268900(29% of the surface) square miles.’,
‘Ain't no numbers in this here words, nohow, no way, Jose.’,
‘James was never known as 0000000007’,
‘Arthur Eddington wrote: I believe there are ’""
‘15747724136275002577605653961181555468044717914527116709366231425076185631031296’""
‘ protons in the universe.’,
‘ $-140000±100 millions.’,
‘6/9/1946 was a good year for some.’]

print(commatize(tests[0], period' 2, sep' ‘*’))
print(commatize(tests[1], period' 3, sep' ‘-’))
print(commatize(tests[2], period' 4, sep' ‘__’))
print(commatize(tests[3], period' 5, sep' ‘ ’))
print(commatize(tests[4], sep' ‘.’))

L(test) tests[5..]
print(commatize(test))</lang>

{{out}}
<pre>
1*23*45*67*89.12*34*56*78*9
.123-456-789
5__7256.1D-4
pi=3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59231
The author has two Z$100.000.000.000.000 Zimbabwe notes (100 trillion).
-in Aus$+1,411.8millions
===US$0017,440 millions=== (in 2000 dollars)
123.e8000 is pretty big.
The land area of the earth is 57,268,900(29% of the surface) square miles.
Ain't no numbers in this here words, nohow, no way, Jose.
James was never known as 0000000007
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.
$-140,000±100 millions.
6/9/1946 was a good year for some.
</pre>


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==