Suffixation of decimal numbers: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: added syntax colouring the hard way)
m (syntax highlighting fixup automation)
Line 122:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F suffize(numstr, digits = -1, base = 10)
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’)</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight lang="go">package main
 
import (
Line 300:
suffize(test)
}
}</langsyntaxhighlight>
 
{{out}}
Line 371:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Printf
 
const suf = Dict{BigInt, String}(BigInt(1) => "", BigInt(10)^100 => "googol",
Line 432:
(n > 2) ? lpad(l[3], 3) : " ", " : ", s)
end
</langsyntaxhighlight>{{out}}
<pre>
87,654,321 : 87.654321M
Line 448:
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight Nimlang="nim">import math, strutils
 
const
Line 504:
suffize("+16777216", base = 2)
echo "[1.2e101]: ",
suffize("1.2e101")</langsyntaxhighlight>
 
{{out}}
Line 520:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use List::Util qw(min max first);
 
sub sufficate {
Line 597:
);
 
printf "%33s : %s\n", $_, sufficate(split ' ', $_) for @tests;</langsyntaxhighlight>
{{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.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{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.
<langsyntaxhighlight lang="python">
import math
import os
Line 746:
for test in tests:
print(' '.join(str(i) for i in test) + ' : ' + suffize(*test))
</syntaxhighlight>
</lang>
 
{{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" perl6line>sub sufficate ($val is copy, $type is copy = 'M', $round is copy = Any) {
if +$type ~~ Int { $round = $type; $type = 'M' }
my $s = '';
Line 825:
;
 
printf "%33s : %s\n", $_, sufficate(|.words) for @tests;</langsyntaxhighlight>
{{out}}
<pre> 87,654,321 : 87.654321M
Line 842:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program to add a (either metric or "binary" metric) suffix to a decimal number.*/
@.= /*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.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; 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.
<langsyntaxhighlight lang="vb">Private Function suffize(number As String, Optional sfractiondigits As String, Optional base As String) As String
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</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default inputs:}}
<pre>------------------------------------------------
Line 1,161:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/big" for BigRat
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)</langsyntaxhighlight>
 
{{out}}
Line 1,313:
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library (big ints)
Error checking is nonexistent.
<langsyntaxhighlight lang="zkl">var [const] BI=Import.lib("zklBigNum"); // GMP
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))
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">testCases:=T(
"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();
}</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits