Suffixation of decimal numbers: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|Phix}}: added syntax colouring the hard way) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 122:
{{trans|Python}}
<
V suffixes = [‘’, ‘K’, ‘M’, ‘G’, ‘T’, ‘P’, ‘E’, ‘Z’, ‘Y’, ‘X’, ‘W’, ‘V’, ‘U’, ‘googol’]
Line 165:
p(‘456,789,100,000.000e+00’, 0, 10)
p(‘+16777216’, -1, 2)
p(‘1.2e101’)</
{{out}}
Line 182:
=={{header|Go}}==
As go doesn't support either function overloading or optional arguments, we just pass a single string to the suffize function and then split out what we need.
<
import (
Line 300:
suffize(test)
}
}</
{{out}}
Line 371:
=={{header|Julia}}==
<
const suf = Dict{BigInt, String}(BigInt(1) => "", BigInt(10)^100 => "googol",
Line 432:
(n > 2) ? lpad(l[3], 3) : " ", " : ", s)
end
</
<pre>
87,654,321 : 87.654321M
Line 448:
=={{header|Nim}}==
{{trans|Python}}
<
const
Line 504:
suffize("+16777216", base = 2)
echo "[1.2e101]: ",
suffize("1.2e101")</
{{out}}
Line 520:
=={{header|Perl}}==
{{trans|Raku}}
<
sub sufficate {
Line 597:
);
printf "%33s : %s\n", $_, sufficate(split ' ', $_) for @tests;</
{{out}}
<pre> 87,654,321 : 87.654321M
Line 618:
at least with any fraction that is not an exact sum of half, quarter, etc, so for
that reason this uses bigatom, since that can hold numbers with absolute accuracy.
<!--<
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">/</span><span style="color: #000000;">bigatom</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 680:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%30s : %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 701:
Tested in Python 3.7.3<br />
Chose 3 places after decimal (where applicable) as default rounding precision. Number to suffize taken as a string. There are some edge cases where this fails due to binary arithmetic differing from decimal arithmetic and things not rounding nicely.
<
import math
import os
Line 746:
for test in tests:
print(' '.join(str(i) for i in test) + ' : ' + suffize(*test))
</syntaxhighlight>
{{out}}
Line 773:
If you desire the number to be rounded, pass in a number representing the placed past the decimal to round to. If you pass in a negative number for rounding, it will round to a negative number of places past the decimal.
<syntaxhighlight lang="raku"
if +$type ~~ Int { $round = $type; $type = 'M' }
my $s = '';
Line 825:
;
printf "%33s : %s\n", $_, sufficate(|.words) for @tests;</
{{out}}
<pre> 87,654,321 : 87.654321M
Line 842:
=={{header|REXX}}==
<
@.= /*default value for the stemmed array. */
parse arg @.1 /*obtain optional arguments from the CL*/
Line 904:
if n=0 then j=0 /*N = 0? Don't use any suffix. */
return sig||strip(n||substr(@, j+1,1))!.b /*add sign, suffixes, strip blanks.*/</
{{out|output|text= when using the internal default inputs:}}
Line 969:
=={{header|VBA}}==
VBA has support for 64 bit integers on 64 bit platforms. The program below was developed on a 32 bit platform.
<
Dim suffix As String, parts() As String, exponent As String
Dim fractiondigits As Integer, nsuffix As Integer, flag As Boolean
Line 1,104:
Debug.Print " new number = "; suffize(tt(0), f, r)
Next i
End Sub</
{{out|output|text= when using the internal default inputs:}}
<pre>------------------------------------------------
Line 1,161:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 1,240:
"1e39", // there isn't a big enough suffix for this one but it's less than googol
]
for (test in tests) suffize.call(test)</
{{out}}
Line 1,313:
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library (big ints)
Error checking is nonexistent.
<
var metric, binary, googol=BI("1e100");
metric,binary = metricBin();
Line 1,357:
.zipWith(b.append,[10..10*(ss.len()),10].apply(BI(2).pow)); // Binary
return(m.filter22("".isType), b.filter22("".isType)); # split to ((strings),(nums))
}</
<
"87,654,321",
"-998,877,665,544,332,211,000 3",
Line 1,380:
test=test.split();
"%33s : %s".fmt(test.concat(" "),sufficate(test.xplode())).println();
}</
{{out}}
<pre>
|