1 | unit UPrefixMultiplier;
|
---|
2 |
|
---|
3 | // Date: 2010-06-01
|
---|
4 |
|
---|
5 | {$mode delphi}
|
---|
6 |
|
---|
7 | interface
|
---|
8 |
|
---|
9 | uses
|
---|
10 | Classes, SysUtils, Math;
|
---|
11 |
|
---|
12 | type
|
---|
13 | TPrefixMultiplierItem = record
|
---|
14 | ShortText: string;
|
---|
15 | FullText: string;
|
---|
16 | Value: Double;
|
---|
17 | end;
|
---|
18 |
|
---|
19 | TPrefixMultiplierDef = array[0..16] of TPrefixMultiplierItem;
|
---|
20 |
|
---|
21 | { TPrefixMultiplier }
|
---|
22 |
|
---|
23 | TPrefixMultiplier = class(TComponent)
|
---|
24 | private
|
---|
25 | function TruncateDigits(Value: Double; Digits: Integer = 3): Double;
|
---|
26 | public
|
---|
27 | function Add(Value: Double; PrefixMultipliers: TPrefixMultiplierDef;
|
---|
28 | UnitText: string; Digits: Integer = 3): string;
|
---|
29 | end;
|
---|
30 |
|
---|
31 | const
|
---|
32 | BasePrefixMultipliers: TPrefixMultiplierDef =
|
---|
33 | (
|
---|
34 | (ShortText: 'y'; FullText: 'yocto'; Value: 1e-24),
|
---|
35 | (ShortText: 'z'; FullText: 'zepto'; Value: 1e-21),
|
---|
36 | (ShortText: 'a'; FullText: 'atto'; Value: 1e-18),
|
---|
37 | (ShortText: 'f'; FullText: 'femto'; Value: 1e-15),
|
---|
38 | (ShortText: 'p'; FullText: 'piko'; Value: 1e-12),
|
---|
39 | (ShortText: 'n'; FullText: 'nano'; Value: 1e-9),
|
---|
40 | (ShortText: 'u'; FullText: 'mikro'; Value: 1e-6),
|
---|
41 | (ShortText: 'm'; FullText: 'mili'; Value: 1e-3),
|
---|
42 | (ShortText: ''; FullText: ''; Value: 1e0),
|
---|
43 | (ShortText: 'k'; FullText: 'kilo'; Value: 1e3),
|
---|
44 | (ShortText: 'M'; FullText: 'mega'; Value: 1e6),
|
---|
45 | (ShortText: 'G'; FullText: 'giga'; Value: 1e9),
|
---|
46 | (ShortText: 'T'; FullText: 'tera'; Value: 1e12),
|
---|
47 | (ShortText: 'P'; FullText: 'peta'; Value: 1e15),
|
---|
48 | (ShortText: 'E'; FullText: 'exa'; Value: 1e18),
|
---|
49 | (ShortText: 'Z'; FullText: 'zetta'; Value: 1e21),
|
---|
50 | (ShortText: 'Y'; FullText: 'yotta'; Value: 1e24)
|
---|
51 | );
|
---|
52 |
|
---|
53 | TimePrefixMultipliers: TPrefixMultiplierDef =
|
---|
54 | (
|
---|
55 | (ShortText: 'ys'; FullText: 'yocto'; Value: 1e-24),
|
---|
56 | (ShortText: 'zs'; FullText: 'zepto'; Value: 1e-21),
|
---|
57 | (ShortText: 'as'; FullText: 'atto'; Value: 1e-18),
|
---|
58 | (ShortText: 'fs'; FullText: 'femto'; Value: 1e-15),
|
---|
59 | (ShortText: 'ps'; FullText: 'piko'; Value: 1e-12),
|
---|
60 | (ShortText: 'ns'; FullText: 'nano'; Value: 1e-9),
|
---|
61 | (ShortText: 'us'; FullText: 'mikro'; Value: 1e-6),
|
---|
62 | (ShortText: 'ms'; FullText: 'mili'; Value: 1e-3),
|
---|
63 | (ShortText: 's'; FullText: 'sekunda'; Value: 1),
|
---|
64 | (ShortText: 'min'; FullText: 'minuta'; Value: 60),
|
---|
65 | (ShortText: 'hod'; FullText: 'hodina'; Value: 3600),
|
---|
66 | (ShortText: 'den'; FullText: 'den'; Value: 24 * 3600),
|
---|
67 | (ShortText: 'týd'; FullText: 'týden'; Value: 7 * 24 * 3600),
|
---|
68 | (ShortText: 'měs'; FullText: 'měsíc'; Value: 30 * 24 * 3600),
|
---|
69 | (ShortText: 'rok'; FullText: 'rok'; Value: 365 * 24 * 3600),
|
---|
70 | (ShortText: 'století'; FullText: 'století'; Value: 3153600000),
|
---|
71 | (ShortText: 'tisíciletí'; FullText: 'tisíciletí'; Value: 3153600000)
|
---|
72 | );
|
---|
73 |
|
---|
74 | procedure Register;
|
---|
75 |
|
---|
76 |
|
---|
77 | implementation
|
---|
78 |
|
---|
79 | procedure Register;
|
---|
80 | begin
|
---|
81 | RegisterComponents('Common', [TPrefixMultiplier]);
|
---|
82 | end;
|
---|
83 |
|
---|
84 | { TPrefixMultiplier }
|
---|
85 |
|
---|
86 | function TPrefixMultiplier.TruncateDigits(Value: Double; Digits: Integer = 3): Double;
|
---|
87 | var
|
---|
88 | II: Integer;
|
---|
89 | RealDigits: Integer;
|
---|
90 | begin
|
---|
91 | Result := 0;
|
---|
92 | for II := 2 downto -1 do begin
|
---|
93 | if Value >= Power(10, II) then begin
|
---|
94 | if Digits < (II + 1) then RealDigits := II + 1
|
---|
95 | else RealDigits := Digits;
|
---|
96 | Result := Round(Value / Power(10, II - RealDigits + 1)) * Power(10, II - RealDigits + 1);
|
---|
97 | Break;
|
---|
98 | end;
|
---|
99 | end;
|
---|
100 | end;
|
---|
101 |
|
---|
102 | function TPrefixMultiplier.Add(Value: Double; PrefixMultipliers: TPrefixMultiplierDef
|
---|
103 | ; UnitText:string; Digits: Integer): string;
|
---|
104 | var
|
---|
105 | I: Integer;
|
---|
106 | begin
|
---|
107 | if UnitText = '' then Result := FloatToStr(TruncateDigits(Value, Digits));
|
---|
108 | I := 8;
|
---|
109 | if Value = 0 then
|
---|
110 | else
|
---|
111 | if Value > 1 then begin
|
---|
112 | while(((Value / PrefixMultipliers[I + 1].Value) > 1) and ((I + 1) >= 0) and
|
---|
113 | ((I + 1) <= Length(PrefixMultipliers))) do Inc(I);
|
---|
114 | end else
|
---|
115 | if Value < 1 then begin
|
---|
116 | while(((Value / PrefixMultipliers[I + 1].Value) < 1) and ((I - 1) >= 0) and
|
---|
117 | ((I - 1) <= Length(PrefixMultipliers))) do Dec(I);
|
---|
118 | Inc(I);
|
---|
119 | end;
|
---|
120 | Value := Value / PrefixMultipliers[I].Value;
|
---|
121 |
|
---|
122 | // Truncate digits count
|
---|
123 | Result := FloatToStr(TruncateDigits(Value, Digits)) + ' ' +
|
---|
124 | PrefixMultipliers[I].ShortText + UnitText;
|
---|
125 | end;
|
---|
126 |
|
---|
127 | end.
|
---|
128 |
|
---|