Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
enum SI {
k("kilo"),
M("mega"),
G("giga"),
T("tera");
String s;
static double y = log(1_000);
SI(String s) { this.s = s; }
static SI get(double x) {
int y = (int) (log(x) / SI.y);
return values()[y - 1];
}
}
SI si = SI.get(a);
a = a / pow(1_000, si.ordinal() + 1);
String x = si.name();
function GetSIPrefix(AValue: Double): string;
const
SIPrefixes: array[1..24] of String = ('Q','R','Y', 'Z','E','P','T','G','M','K','H','D','','d','c','m','ยต','mc','n','p','f','a','z','y');
SIFactors: array[1..24] of Double = (1E+30,1E+27,1E+24, 1E+21, 1E+18, 1E+15, 1E+12, 1E+9, 1E+6, 1E+3,1E+2,1E+1,1,1E-1,1E-2,
var
i: Integer;
begin
Result := '';
for i := 1 to 24 do
begin
if (Abs(AValue) >= SIFactors[i]) then
begin
Exit(SIPrefixes[i]);
end;
end;
end;
m = zip('TGMk', (1e12, 1e9, 1e6, 1e3))
x = None
for k, v in m:
if a >= v:
x = (k, v)
break
x = case a
when 1e3... 1e6 ; "k"
when 1e6... 1e9 ; "M"
when 1e9... 1e12; "G"
when 1e12...1e15; "T"
else ""
end