Suffixation of decimal numbers: Difference between revisions

Added FreeBASIC
(Added FreeBASIC)
 
(12 intermediate revisions by 8 users not shown)
Line 116:
be omitted or varied.
 
{{Template:Strings}}
 
;Related tasks:
:*   [[Numerical and alphabetical suffixes]]
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F suffize(numstr, digits = -1, base = 10)
V suffixes = [‘’, ‘K’, ‘M’, ‘G’, ‘T’, ‘P’, ‘E’, ‘Z’, ‘Y’, ‘X’, ‘W’, ‘V’, ‘U’, ‘googol’]
 
V exponent_distance = I base == 2 {10} E 3
V num_sign = I numstr[0] C ‘+-’ {numstr[0]} E ‘’
 
V num = abs(Float(numstr.replace(‘,’, ‘’)))
 
Int suffix_index
I base == 10 & num >= 1e100
suffix_index = 13
num /= 1e100
E I num > 1
V magnitude = floor(log(num, base))
suffix_index = min(Int(floor(magnitude / exponent_distance)), 12)
num /= Float(base) ^ (exponent_distance * suffix_index)
E
suffix_index = 0
 
String num_str
I digits != -1
num_str = format_float(num, digits)
E
num_str = format_float(num, 3).rtrim(‘0’).rtrim(‘.’)
 
R num_sign‘’num_str‘’suffixes[suffix_index]‘’(I base == 2 {‘i’} E ‘’)
 
F p(num, digits = -1, base = 10)
print(num, end' ‘ ’)
I digits != -1
print(digits, end' ‘ ’)
I base != 10
print(‘base = ’base, end' ‘ ’)
print(‘: ’suffize(num, digits, base))
 
p(‘87,654,321’)
p(‘-998,877,665,544,332,211,000’, 3)
p(‘+112,233’, 0)
p(‘16,777,216’, 1)
p(‘456,789,100,000,000’, 2)
p(‘456,789,100,000,000’, 5, 2)
p(‘456,789,100,000.000e+00’, 0, 10)
p(‘+16777216’, -1, 2)
p(‘1.2e101’)</syntaxhighlight>
 
{{out}}
<pre>
87,654,321 : 87.654M
-998,877,665,544,332,211,000 3 : -998.878E
+112,233 0 : +112K
16,777,216 1 : 16.8M
456,789,100,000,000 2 : 456.79T
456,789,100,000,000 5 base = 2 : 415.44727Ti
456,789,100,000.000e+00 0 : 457G
+16777216 base = 2 : +16Mi
1.2e101 : 12googol
</pre>
 
 
=={{header|C#}}==
{{trans|Python}}
<syntaxhighlight lang="C#">
using System;
using System.Globalization;
 
public class NumberSuffixer
{
private static readonly string[] Suffixes = { "", "K", "M", "G", "T", "P", "E", "Z", "Y", "X", "W", "V", "U", "googol" };
 
public static string Suffize(string num, int? digits = null, int baseNum = 10)
{
int exponentDistance = baseNum == 2 ? 10 : 3;
num = num.Trim().Replace(",", "");
string numSign = num[0] == '+' || num[0] == '-' ? num.Substring(0, 1) : "";
 
num = numSign != "" ? num.Substring(1) : num;
double number = Math.Abs(double.Parse(num, CultureInfo.InvariantCulture));
 
int suffixIndex;
if (baseNum == 10 && number >= 1e100)
{
suffixIndex = 13;
number /= 1e100;
}
else if (number > 1)
{
double magnitude = Math.Floor(Math.Log(number, baseNum));
suffixIndex = Math.Min((int)Math.Floor(magnitude / exponentDistance), 12);
number /= Math.Pow(baseNum, exponentDistance * suffixIndex);
}
else
{
suffixIndex = 0;
}
 
string numStr;
if (digits.HasValue)
{
numStr = number.ToString($"F{digits}", CultureInfo.InvariantCulture);
}
else
{
numStr = number.ToString("F3", CultureInfo.InvariantCulture).TrimEnd('0').TrimEnd('.');
}
 
return numSign + numStr + Suffixes[suffixIndex] + (baseNum == 2 ? "i" : "");
}
 
public static void Main(string[] args)
{
var tests = new (string, int?, int?)[]
{
("87,654,321", null, null),
("-998,877,665,544,332,211,000", 3, null),
("+112,233", 0, null),
("16,777,216", 1, null),
("456,789,100,000,000", 2, null),
("456,789,100,000,000", 2, 10),
("456,789,100,000,000", 5, 2),
("456,789,100,000.000e+00", 0, 10),
("+16777216", null, 2),
("1.2e101", null, null),
};
 
foreach (var test in tests)
{
Console.WriteLine($"{test.Item1}, {test.Item2?.ToString() ?? "null"}, {test.Item3?.ToString() ?? "null"} : " +
$"{Suffize(test.Item1, test.Item2, test.Item3 ?? 10)}");
}
}
}
</syntaxhighlight>
{{out}}
<pre>
87,654,321, null, null : 87.654M
-998,877,665,544,332,211,000, 3, null : -998.878E
+112,233, 0, null : +112K
16,777,216, 1, null : 16.8M
456,789,100,000,000, 2, null : 456.79T
456,789,100,000,000, 2, 10 : 456.79T
456,789,100,000,000, 5, 2 : 415.44727Ti
456,789,100,000.000e+00, 0, 10 : 457G
+16777216, null, 2 : +16Mi
1.2e101, null, null : 12googol
 
</pre>
 
=={{header|FreeBASIC}}==
{{trans|Nim}}
<syntaxhighlight lang="vbnet">#include "string.bi"
 
#define MIN(a, b) iif((a) < (b), (a), (b))
 
Dim Shared As String*6 Suffixes(13) = {"", "K", "M", "G", "T", "P", "E", "Z", "Y", "X", "W", "V", "U", "googol"}
 
Function ReplaceString(Byval s As String, Byval find As String, Byval replaceWith As String) As String
Dim posic As Integer
posic = Instr(s, find)
While posic > 0
s = Left(s, posic - 1) & replaceWith & Mid(s, posic + Len(find))
posic = Instr(posic + Len(replaceWith), s, find)
Wend
Return s
End Function
 
Function suffize(num As String, digits As Integer, bbase As Integer) As String
Dim As Integer exponentDist
exponentDist = Iif(bbase = 2, 10, 3)
Dim As String numSign = ""
If Left(num, 1) = "+" Or Left(num, 1) = "-" Then
numSign = Left(num, 1)
num = Mid(num, 2)
End If
num = ReplaceString(num, ",", "")
Dim As Double n = Val(num)
Dim As Integer suffixIndex
If bbase = 10 And n >= 1e100 Then
suffixIndex = 13
n /= 1e100
Elseif n > 1 Then
Dim As Integer magnitude = Int(Log(n) / Log(bbase))
suffixIndex = MIN(magnitude \ exponentDist, 12)
n /= bbase ^ (exponentDist * suffixIndex)
Else
suffixIndex = 0
End If
Dim As String numStr
If digits > 0 Then
numStr = Format(n, "0." & String(digits, "#"))
Elseif digits = 0 Then
numStr = Format(Int(n), "0")
Else
numStr = Format(n, "0.###")
End If
Return numSign & numStr & Suffixes(suffixIndex) & Iif(bbase = 2, "i", "")
End Function
 
Print "[87,654,321]: "; suffize("87,654,321", 3, 10)
Print "[-998,877,665,544,332,211,000 / digits = 3]: "; suffize("-998877665544332211000", 3, 10)
Print "[+112,233 / digits = 0]: "; suffize("+112233", 0, 10)
Print "[16,777,216 / digits = 1]: "; suffize("16777216", 1, 10)
Print "[456,789,100,000,000 / digits = 2]: "; suffize("456789100000000", 2, 10)
Print "[456,789,100,000,000 / digits = 2 / base = 10]: "; suffize("456789100000000", 2, 10)
Print "[456,789,100,000,000 / digits = 5 / base = 2]: "; suffize("456789100000000", 5, 2)
Print "[456,789,100,000.000e+000 / digits = 0 / base = 10]: "; suffize("456789100000", 0, 10)
Print "[+16777216 / base = 2]: "; suffize("+16777216", 3, 2)
Print "[1.2e101]: "; suffize("1.2e101", 3, 10)
 
Sleep</syntaxhighlight>
 
=={{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 241 ⟶ 457:
suffize(test)
}
}</langsyntaxhighlight>
 
{{out}}
Line 310 ⟶ 526:
new number = 1000U
</pre>
 
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Printf
 
const suf = Dict{BigInt, String}(BigInt(1) => "", BigInt(10)^100 => "googol",
Line 374 ⟶ 589:
(n > 2) ? lpad(l[3], 3) : " ", " : ", s)
end
</langsyntaxhighlight>{{out}}
<pre>
87,654,321 : 87.654321M
Line 387 ⟶ 602:
1.2e101 : 12googol
</pre>
 
=={{header|Nim}}==
{{trans|Python}}
<syntaxhighlight lang="nim">import math, strutils
 
const
Suffixes = ["", "K", "M", "G", "T", "P", "E", "Z", "Y", "X", "W", "V", "U", "googol"]
None = -1
 
proc suffize(num: string; digits = None; base = 10): string =
 
let exponentDist = if base == 2: 10 else: 3
let num = num.strip().replace(",", "")
let numSign = if num[0] in {'+', '-'}: $num[0] else: ""
 
var n = abs(num.parseFloat())
var suffixIndex: int
if base == 10 and n >= 1e100:
suffixIndex = 13
n /= 1e100
elif n > 1:
let magnitude = log(n, base.toFloat).int
suffixIndex = min(magnitude div exponentDist, 12)
n /= float(base ^ (exponentDist * suffixIndex))
else:
suffixIndex = 0
 
let numStr = if digits > 0:
n.formatFloat(ffDecimal, precision = digits)
elif digits == 0:
# Can’t use "formatFloat" with precision = 0 as it keeps the decimal point.
# So convert to nearest int and format this value.
$(n.toInt)
else:
n.formatFloat(ffDecimal, precision = 3).strip(chars = {'0'}).strip(chars = {'.'})
result = numSign & numStr & Suffixes[suffixIndex] & (if base == 2: "i" else: "")
 
 
when isMainModule:
 
echo "[87,654,321]: ",
suffize("87,654,321")
echo "[-998,877,665,544,332,211,000 / digits = 3]: ",
suffize("-998,877,665,544,332,211,000", 3)
echo "[+112,233 / digits = 0]: ",
suffize("+112,233", 0)
echo "[16,777,216 / digits = 1]: ",
suffize("16,777,216", 1)
echo "[456,789,100,000,000 / digits = 2]: ",
suffize("456,789,100,000,000", 2)
echo "[456,789,100,000,000 / digits = 2 / base = 10]: ",
suffize("456,789,100,000,000", 2, 10)
echo "[456,789,100,000,000 / digits = 5 / base = 2]: ",
suffize("456,789,100,000,000", digits = 5, base = 2)
echo "[456,789,100,000.000e+000 / digits = 0 / base = 10]: ",
suffize("456,789,100,000.000e+000", digits = 0, base = 10)
echo "[+16777216 / base = 2]: ",
suffize("+16777216", base = 2)
echo "[1.2e101]: ",
suffize("1.2e101")</syntaxhighlight>
 
{{out}}
<pre>[87,654,321]: 87.654M
[-998,877,665,544,332,211,000 / digits = 3]: -998.878E
[+112,233 / digits = 0]: +112K
[16,777,216 / digits = 1]: 16.8M
[456,789,100,000,000 / digits = 2]: 456.79T
[456,789,100,000,000 / digits = 2 / base = 10]: 456.79T
[456,789,100,000,000 / digits = 5 / base = 2]: 415.44727Ti
[456,789,100,000.000e+000 / digits = 0 / base = 10]: 457G
[+16777216 / base = 2]: +16Mi
[1.2e101]: 12googol</pre>
 
=={{header|Perl}}==
{{trans|Perl 6Raku}}
<langsyntaxhighlight lang="perl">use List::Util qw(min max first);
 
sub sufficate {
Line 467 ⟶ 754:
);
 
printf "%33s : %s\n", $_, sufficate(split ' ', $_) for @tests;</langsyntaxhighlight>
{{out}}
<pre> 87,654,321 : 87.654321M
Line 482 ⟶ 769:
1122334455 Q : What we have here is a failure to communicate...</pre>
 
=={{header|Perl 6Phix}}==
The builtin routine file_size_k() handles KB/MB/GB/TB suffixes.
 
Note that you simply cannot "display all significant digits" when using IEEE-754,
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.
<!--<syntaxhighlight lang="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>
<span style="color: #008080;">constant</span> <span style="color: #000000;">suffixes</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"KMGTPEZYXWVU"</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">anydp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">100</span> <span style="color: #000080;font-style:italic;">-- the "any dp" value (prevents nearest 1e-100, though)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">suffize</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">args</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">bigatom</span> <span style="color: #000000;">size</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ba_abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">args</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">sgn</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ba_compare</span><span style="color: #0000FF;">(</span><span style="color: #000000;">args</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">0</span><span style="color: #0000FF;">?-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">:+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">la</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">args</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">dp</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">la</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">2</span><span style="color: #0000FF;">?</span><span style="color: #000000;">args</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]:</span><span style="color: #000000;">anydp</span><span style="color: #0000FF;">),</span> <span style="color: #000080;font-style:italic;">-- decimal places</span>
<span style="color: #000000;">bd</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">la</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">3</span><span style="color: #0000FF;">?</span><span style="color: #000000;">args</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</span><span style="color: #0000FF;">]:</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- 2 or 10</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">ip</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (inverted precision)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">dp</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">size</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ba_round</span><span style="color: #0000FF;">(</span><span style="color: #000000;">size</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ip</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">suffix</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ba_compare</span><span style="color: #0000FF;">(</span><span style="color: #000000;">size</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1e100</span><span style="color: #0000FF;">)></span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">size</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ba_div</span><span style="color: #0000FF;">(</span><span style="color: #000000;">size</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1e100</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">suffix</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"googol"</span>
<span style="color: #008080;">else</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">factor</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bd</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span><span style="color: #0000FF;">?</span><span style="color: #000000;">1024</span><span style="color: #0000FF;">:</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">fdx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">fdx</span><span style="color: #0000FF;"><</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">suffixes</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">bigatom</span> <span style="color: #000000;">rsize</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ba_div</span><span style="color: #0000FF;">(</span><span style="color: #000000;">size</span><span style="color: #0000FF;">,</span><span style="color: #000000;">factor</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">dp</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">rsize</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ba_round</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rsize</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ip</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ba_compare</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rsize</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">size</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rsize</span>
<span style="color: #000000;">fdx</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #000000;">suffix</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fdx</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">?</span><span style="color: #008000;">""</span><span style="color: #0000FF;">:</span><span style="color: #000000;">suffixes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">fdx</span><span style="color: #0000FF;">]&</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bd</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"i"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">""</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">fmt</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dp</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">or</span> <span style="color: #000000;">dp</span><span style="color: #0000FF;">=</span><span style="color: #000000;">anydp</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"%0B"</span><span style="color: #0000FF;">:</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%%0.%dB"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">))</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ba_sprintf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ba_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">size</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sgn</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">suffix</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">test_text</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
87,654,321
-998,877,665,544,332,211,000 3
+112,233 0
16,777,216 1
456,789,100,000,000
456,789,100,000,000 2 10
456,789,100,000,000 5 2
456,789,100,000.000e+00 0 10
16777216 , 2
1.2e101
347,344 -2
10
"""</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">test_cases</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">test_text</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'\n'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">no_empty</span><span style="color: #0000FF;">:=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">test_cases</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">ti</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">test_cases</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">args</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">,</span><span style="color: #000000;">no_empty</span><span style="color: #0000FF;">:=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">args</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ba_new</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">args</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #008000;">","</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">args</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">sa</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">args</span><span style="color: #0000FF;">[</span><span style="color: #000000;">a</span><span style="color: #0000FF;">],</span><span style="color: #008000;">"%f"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">args</span><span style="color: #0000FF;">[</span><span style="color: #000000;">a</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sa</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #000000;">sa</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]:</span><span style="color: #000000;">anydp</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">suffize</span><span style="color: #0000FF;">(</span><span style="color: #000000;">args</span><span style="color: #0000FF;">)</span>
<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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
87,654,321 : 87.654321M
-998,877,665,544,332,211,000 3 : -998.878E
+112,233 0 : 112K
16,777,216 1 : 16.8M
456,789,100,000,000 : 456.7891T
456,789,100,000,000 2 10 : 456.79T
456,789,100,000,000 5 2 : 415.44727Ti
456,789,100,000.000e+00 0 10 : 457G
16777216 , 2 : 16Mi
1.2e101 : 12googol
347,344 -2 : 300K
10 : 10
</pre>
 
=={{header|Python}}==
{{works with|cpython|3.7.3}}
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.
<syntaxhighlight lang="python">
import math
import os
 
 
def suffize(num, digits=None, base=10):
suffixes = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'X', 'W', 'V', 'U', 'googol']
 
exponent_distance = 10 if base == 2 else 3
num = num.strip().replace(',', '')
num_sign = num[0] if num[0] in '+-' else ''
 
num = abs(float(num))
 
if base == 10 and num >= 1e100:
suffix_index = 13
num /= 1e100
elif num > 1:
magnitude = math.floor(math.log(num, base))
suffix_index = min(math.floor(magnitude / exponent_distance), 12)
num /= base ** (exponent_distance * suffix_index)
else:
suffix_index = 0
 
if digits is not None:
num_str = f'{num:.{digits}f}'
else:
num_str = f'{num:.3f}'.strip('0').strip('.')
 
return num_sign + num_str + suffixes[suffix_index] + ('i' if base == 2 else '')
 
 
tests = [('87,654,321',),
('-998,877,665,544,332,211,000', 3),
('+112,233', 0),
('16,777,216', 1),
('456,789,100,000,000', 2),
('456,789,100,000,000', 2, 10),
('456,789,100,000,000', 5, 2),
('456,789,100,000.000e+00', 0, 10),
('+16777216', None, 2),
('1.2e101',)]
 
for test in tests:
print(' '.join(str(i) for i in test) + ' : ' + suffize(*test))
</syntaxhighlight>
 
{{out}}
<pre>
87,654,321 : 87.654M
-998,877,665,544,332,211,000 3 : -998.878E
+112,233 0 : +112K
16,777,216 1 : 16.8M
456,789,100,000,000 2 : 456.79T
456,789,100,000,000 2 10 : 456.79T
456,789,100,000,000 5 2 : 415.44727Ti
456,789,100,000.000e+00 0 10 : 457G
+16777216 None 2 : +16Mi
1.2e101 : 12googol
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2018.09}}
Pass in a number string, optionally a type, and optionally the number of digits to round to.
Line 492 ⟶ 930:
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 544 ⟶ 982:
;
 
printf "%33s : %s\n", $_, sufficate(|.words) for @tests;</langsyntaxhighlight>
{{out}}
<pre> 87,654,321 : 87.654321M
Line 559 ⟶ 997:
347,344 M -2 : 300K
1122334455 Q : What we have here is a failure to communicate...</pre>
 
=={{header|Phix}}==
The builtin routine file_size_k() handles KB/MB/GB/TB suffixes.
 
Note that you simply cannot "display all significant digits" when using IEEE-754,
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.
<lang Phix>include builtins/bigatom.e
 
constant suffixes = "KMGTPEZYXWVU",
anydp = 100 -- the "any dp" value (prevents nearest 1e-100, though)
 
function suffize(sequence args)
bigatom size = ba_abs(args[1])
integer sgn = iff(ba_compare(args[1],0)<0?-1:+1),
la = length(args),
dp = iff(la>=2?args[2]:anydp), -- decimal places
bd = iff(la>=3?args[3]:10) -- 2 or 10
atom ip = power(10,dp) -- (inverted precision)
if dp<0 then size = ba_round(size,ip) end if
string suffix
if ba_compare(size,1e100)>0 then
size = ba_div(size,1e100)
suffix = "googol"
else
integer factor = iff(bd=2?1024:1000), fdx = 0
while fdx<length(suffixes) do
bigatom rsize = ba_div(size,factor)
if dp<0 then rsize = ba_round(rsize,ip) end if
if ba_compare(rsize,1)<0 then exit end if
size = rsize
fdx += 1
end while
suffix = iff(fdx=0?"":suffixes[fdx]&iff(bd=2?"i":""))
end if
string fmt = iff(dp<0 or dp=anydp?"%0B":sprintf("%%0.%dB",dp))
string res = ba_sprintf(fmt, ba_mul(size,sgn))
res &= suffix
return res
end function
 
constant test_text = """
87,654,321
-998,877,665,544,332,211,000 3
+112,233 0
16,777,216 1
456,789,100,000,000
456,789,100,000,000 2 10
456,789,100,000,000 5 2
456,789,100,000.000e+00 0 10
16777216 , 2
1.2e101
347,344 -2
10
""",
test_cases = split(test_text,'\n',no_empty:=true)
 
for i=1 to length(test_cases) do
string ti = test_cases[i]
sequence args = split(ti,no_empty:=true)
args[1] = ba_new(substitute(args[1],",",""))
for a=2 to length(args) do
sequence sa = scanf(args[a],"%f")
args[a] = iff(length(sa)=1?sa[1][1]:anydp)
end for
string res = suffize(args)
printf(1,"%30s : %s\n",{ti,res})
end for</lang>
{{out}}
<pre>
87,654,321 : 87.654321M
-998,877,665,544,332,211,000 3 : -998.878E
+112,233 0 : 112K
16,777,216 1 : 16.8M
456,789,100,000,000 : 456.7891T
456,789,100,000,000 2 10 : 456.79T
456,789,100,000,000 5 2 : 415.44727Ti
456,789,100,000.000e+00 0 10 : 457G
16777216 , 2 : 16Mi
1.2e101 : 12googol
347,344 -2 : 300K
10 : 10
</pre>
 
=={{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 706 ⟶ 1,061:
 
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 771 ⟶ 1,126:
=={{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 906 ⟶ 1,261:
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 958 ⟶ 1,313:
specified radix =
new number = 12googol</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./big" for BigRat
import "./fmt" for Fmt
 
var suffixes = " KMGTPEZYXWVU"
var googol = BigRat.fromDecimal("1e100")
 
var suffize = Fn.new { |arg|
var fields = arg.split(" ").where { |s| s != "" }.toList
if (fields.isEmpty) fields.add("0")
var a = fields[0]
var places
var base
var frac = ""
var radix = ""
var fc = fields.count
if (fc == 1) {
places = -1
base = 10
} else if (fc == 2) {
places = Num.fromString(fields[1])
base = 10
frac = places.toString
} else if (fc == 3) {
if (fields[1] == ",") {
places = 0
frac = ","
} else {
places = Num.fromString(fields[1])
frac = places.toString
}
base = Num.fromString(fields[2])
if (base !=2 && base != 10) base = 10
radix = base.toString
}
a = a.replace(",", "") // get rid of any commas
var sign = ""
if (a[0] == "+" || a[0] == "-") {
sign = a[0]
a = a[1..-1] // remove any sign after storing it
}
var b = BigRat.fromDecimal(a)
var g = b >= googol
var d = (!g && base == 2) ? BigRat.new(1024, 1) :
(!g && base == 10) ? BigRat.new(1000, 1) : googol.copy()
var c = 0
while (b >= d && c < 12) { // allow b >= 1K if c would otherwise exceed 12
b = b / d
c = c + 1
}
var suffix = !g ? suffixes[c] : "googol"
if (base == 2) suffix = suffix + "i"
System.print(" input number = %(fields[0])")
System.print(" fraction digs = %(frac)")
System.print("specified radix = %(radix)")
System.write(" new number = ")
BigRat.showAsInt = true
if (places >= 0) {
Fmt.print("$0s$s$s", sign, b.toDecimal(places), suffix)
} else {
Fmt.print("$0s$s$s", sign, b.toDecimal, suffix)
}
System.print()
}
 
var tests = [
"87,654,321",
"-998,877,665,544,332,211,000 3",
"+112,233 0",
"16,777,216 1",
"456,789,100,000,000",
"456,789,100,000,000 2 10",
"456,789,100,000,000 5 2",
"456,789,100,000.000e+00 0 10",
"+16777216 , 2",
"1.2e101",
"446,835,273,728 1",
"1e36",
"1e39", // there isn't a big enough suffix for this one but it's less than googol
]
for (test in tests) suffize.call(test)</syntaxhighlight>
 
{{out}}
<pre>
input number = 87,654,321
fraction digs =
specified radix =
new number = 87.654321M
 
input number = -998,877,665,544,332,211,000
fraction digs = 3
specified radix =
new number = -998.878E
 
input number = +112,233
fraction digs = 0
specified radix =
new number = +112K
 
input number = 16,777,216
fraction digs = 1
specified radix =
new number = 16.8M
 
input number = 456,789,100,000,000
fraction digs =
specified radix =
new number = 456.7891T
 
input number = 456,789,100,000,000
fraction digs = 2
specified radix = 10
new number = 456.79T
 
input number = 456,789,100,000,000
fraction digs = 5
specified radix = 2
new number = 415.44727Ti
 
input number = 456,789,100,000.000e+00
fraction digs = 0
specified radix = 10
new number = 457G
 
input number = +16777216
fraction digs = ,
specified radix = 2
new number = +16Mi
 
input number = 1.2e101
fraction digs =
specified radix =
new number = 12googol
 
input number = 446,835,273,728
fraction digs = 1
specified radix =
new number = 446.8G
 
input number = 1e36
fraction digs =
specified radix =
new number = 1U
 
input number = 1e39
fraction digs =
specified radix =
new number = 1000U
</pre>
 
=={{header|zkl}}==
{{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,005 ⟶ 1,514:
.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,028 ⟶ 1,537:
test=test.split();
"%33s : %s".fmt(test.concat(" "),sufficate(test.xplode())).println();
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,047 ⟶ 1,556:
1122334455 , 666 : Invalid suffix
10 : 10
</pre>
 
=={{header|Python}}==
{{works with|cpython|3.7.3}}
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.
<lang python>
import math
import os
 
 
def suffize(num, digits=None, base=10):
suffixes = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'X', 'W', 'V', 'U', 'googol']
 
exponent_distance = 10 if base == 2 else 3
num = num.strip().replace(',', '')
num_sign = num[0] if num[0] in '+-' else ''
 
num = abs(float(num))
 
if base == 10 and num >= 1e100:
suffix_index = 13
num /= 1e100
else:
if num > 1:
magnitude = math.floor(math.log(num, base))
suffix_index = min(math.floor(magnitude / exponent_distance), 12)
num /= base ** (exponent_distance * suffix_index)
else:
suffix_index = 0
 
if digits is not None:
num_str = f'{num:.{digits}f}'
else:
num_str = f'{num:.3f}'.strip('0').strip('.')
 
return num_sign + num_str + suffixes[suffix_index] + ('i' if base == 2 else '')
 
 
tests = [('87,654,321',),
('-998,877,665,544,332,211,000', 3),
('+112,233', 0),
('16,777,216', 1),
('456,789,100,000,000', 2),
('456,789,100,000,000', 2, 10),
('456,789,100,000,000', 5, 2),
('456,789,100,000.000e+00', 0, 10),
('+16777216', None, 2),
('1.2e101',)]
 
for test in tests:
print(' '.join(str(i) for i in test) + ' : ' + suffize(*test))
</lang>
 
{{out}}
<pre>
87,654,321 : 87.654M
-998,877,665,544,332,211,000 3 : -998.878E
+112,233 0 : +112K
16,777,216 1 : 16.8M
456,789,100,000,000 2 : 456.79T
456,789,100,000,000 2 10 : 456.79T
456,789,100,000,000 5 2 : 415.44727Ti
456,789,100,000.000e+00 0 10 : 457G
+16777216 None 2 : +16Mi
1.2e101 : 12googol
</pre>
2,130

edits