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
|
---|
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 | implementation
|
---|
75 |
|
---|
76 | { TPrefixMultiplier }
|
---|
77 |
|
---|
78 | function TPrefixMultiplier.TruncateDigits(Value: Double; Digits: Integer = 3): Double;
|
---|
79 | var
|
---|
80 | II: Integer;
|
---|
81 | RealDigits: Integer;
|
---|
82 | begin
|
---|
83 | Result := 0;
|
---|
84 | for II := 2 downto -1 do begin
|
---|
85 | if Value >= Power(10, II) then begin
|
---|
86 | if Digits < (II + 1) then RealDigits := II + 1
|
---|
87 | else RealDigits := Digits;
|
---|
88 | Result := Round(Value / Power(10, II - RealDigits + 1)) * Power(10, II - RealDigits + 1);
|
---|
89 | Break;
|
---|
90 | end;
|
---|
91 | end;
|
---|
92 | end;
|
---|
93 |
|
---|
94 | function TPrefixMultiplier.Add(Value:Double;PrefixMultipliers:TPrefixMultiplierDef
|
---|
95 | ;UnitText:string;Digits:Integer):string;
|
---|
96 | var
|
---|
97 | I: Integer;
|
---|
98 | begin
|
---|
99 | if UnitText = '' then Result := FloatToStr(TruncateDigits(Value, Digits));
|
---|
100 | I := 8;
|
---|
101 | if Value = 0 then
|
---|
102 | else
|
---|
103 | if Value > 1 then begin
|
---|
104 | while(((Value / PrefixMultipliers[I + 1].Value) > 1) and ((I + 1) >= 0) and
|
---|
105 | ((I + 1) <= Length(PrefixMultipliers))) do Inc(I);
|
---|
106 | end else
|
---|
107 | if Value < 1 then begin
|
---|
108 | while(((Value / PrefixMultipliers[I + 1].Value) < 1) and ((I - 1) >= 0) and
|
---|
109 | ((I - 1) <= Length(PrefixMultipliers))) do Dec(I);
|
---|
110 | Inc(I);
|
---|
111 | end;
|
---|
112 | Value := Value / PrefixMultipliers[I].Value;
|
---|
113 |
|
---|
114 | // Truncate digits count
|
---|
115 | Result := FloatToStr(TruncateDigits(Value, Digits)) + ' ' +
|
---|
116 | PrefixMultipliers[I].ShortText + UnitText;
|
---|
117 | end;
|
---|
118 |
|
---|
119 | end.
|
---|
120 |
|
---|