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