Commatizing numbers: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
No edit summary
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(4 intermediate revisions by 2 users not shown)
Line 955:
$-140,000±100 millions.
6/9/1946 was a good year for some.</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq.'''
<syntaxhighlight lang="jq">
def commatize($s; $start; $step; $sep):
 
def isExponent($c): "eEdDpP^∙x↑*⁰¹²³⁴⁵⁶⁷⁸⁹" | index($c);
def rev: explode|reverse|implode;
def addSeps($n; $dp):
{ $n, lz: "" }
| if ($dp|not) and ($n|startswith("0")) and $n != "0"
then .k = ($n|sub("^0*";""))
| if (.k == "") then .k = "0" else . end
| .lz = "0" * (($n|length) - (.k|length))
| .n = .k
else .
end
| if $dp
then .n |= rev # reverse if after decimal point
else .
end
| .i = (.n|length) - $step
| until (.i < 1;
.n = .n[: .i] + $sep + .n[.i :]
| .i += - $step )
| if $dp
then .n |= rev # reverse again
else .
end
| .lz + .n;
 
{ acc: $s[:$start],
n: "",
dp: false }
| label $out
| foreach (range($start; $s|length), null) as $j (.;
if $j == null then .emit = true
else $s[$j:$j+1] as $x
| ($x | explode[0]) as $c
| if ($c >= 48 and $c <= 57)
then .n += $x
| if $j == (($s|length)-1)
then if (.acc != "" and isExponent(.acc[-1:]))
then .acc = $s
else .acc += addSeps(.n; .dp)
end
else .
end
elif .n != ""
then if (.acc != "" and isExponent(.acc[-1:]))
then .acc = $s
| .emit=true | ., break $out
elif $x != "."
then .acc += addSeps(.n; .dp) + $s[$j:]
| .emit=true | ., break $out
else .acc += addSeps(.n; .dp) + $x
| .dp = true
| .n = ""
end
else .acc += $x
end
end )
| select(.emit)
| $s, .acc, ""
;
 
# Input: the string to be commatized
def commatize:
commatize(.; 0; 3; ",");
 
def defaults: [
"\"-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."
];
 
def exercise:
commatize("123456789.123456789"; 0; 2; "*"),
commatize(".123456789"; 0; 3; "-"),
commatize("57256.1D-4"; 0; 4; "__"),
commatize("pi=3.14159265358979323846264338327950288419716939937510582097494459231"; 0; 5; " "),
commatize("The author has two Z$100000000000000 Zimbabwe notes (100 trillion)."; 0; 3; "."),
 
(defaults[] | commatize) ;
 
exercise
</syntaxhighlight>
{{output}}
Exactly as for [[#Wren|Wren]].
 
=={{header|Julia}}==
Line 1,736 ⟶ 1,833:
$-140,000±100 millions.
6/9/1946 was a good year for some.</pre>
 
=={{header|VBScript}}==
Adapted from the Future Basic code
<syntaxhighlight lang="vb">
function commatize( s , sep, start , stp )
if sep ="" then sep = ","
if start ="" then start = 1
if stp ="" then stp = 3
Dim i, j, k, l
l = len(s)
for i = start to l
if ( asc( mid( s, i, 1 ) ) >= asc("1") and asc( mid( s, i, 1) ) <= asc("9") ) then
for j = i + 1 to l + 1
if ( j > l ) then
for k = j - 1 - stp to i step -stp
s = mid( s, 1, k ) + sep + mid( s, k + 1, l - k + 1 )
l = len(s)
next 'k
exit for
else
if ( asc( mid( s, j, 1 ) ) < asc("0") or asc( mid( s, j, 1 ) ) > asc("9") ) then
for k = j - 1 - stp to i step -stp
s = mid( s, 1, k ) + sep + mid( s, k + 1, l - k + 1 )
l = len(s)
Next ' k
exit for
end if
end if
next 'j
exit for
end if
next '
commatize=S
end function
 
wscript.echo commatize("pi=3.14159265358979323846264338327950288419716939937510582097494459231" , " " , 6, 5 )
wscript.echo commatize("The author has two Z$100000000000000 Zimbabwe notes (100 trillion)." , "." , "", "" )
wscript.echo commatize("\'-in Aus$+1411.8millions\'" , "," , "", "" )
wscript.echo commatize("===US$0017440 millions=== (in 2000 dollars)" , "" , "", "" )
wscript.echo commatize("123.e8000 is pretty big." , "" , "", "" )
wscript.echo commatize("The land area of the earth is 57268900(29% of the surface) square miles." , "" , "", "" )
wscript.echo commatize("Ain't no numbers in this here words, nohow, no way, Jose." , "" , "", "" )
wscript.echo commatize("James was never known as 0000000007" , "" , "", "" )
wscript.echo commatize("Arthur Eddington wrote: I believe there are 15747724136275002577605653961181555468044717914527116709366231425076185631031296 protons in the universe.", ",", "", "" )
wscript.echo commatize(" $-140000±100 millions." , "" , "", "" )
wscript.echo commatize("6/9/1946 was a good year for some." , "" , "", "" )
</syntaxhighlight>
{{out}}
<small>
<pre>
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>
</small>
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var commatize = Fn.new { |s, start, step, sep|
var addSeps = Fn.new { |n, dp|
var lz = ""
9,476

edits