source: trunk/Packages/Common/UPrefixMultiplier.pas

Last change on this file was 18, checked in by chronos, 12 years ago
  • Used external packages are now stored in uncompressed form rather in zipped files. This allow better package version synchronisation.
File size: 3.9 KB
Line 
1unit UPrefixMultiplier;
2
3// Date: 2010-06-01
4
5{$mode delphi}
6
7interface
8
9uses
10 Classes, SysUtils, Math;
11
12type
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
31const
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
74implementation
75
76{ TPrefixMultiplier }
77
78function TPrefixMultiplier.TruncateDigits(Value: Double; Digits: Integer = 3): Double;
79var
80 II: Integer;
81 RealDigits: Integer;
82begin
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;
92end;
93
94function TPrefixMultiplier.Add(Value:Double;PrefixMultipliers:TPrefixMultiplierDef
95 ;UnitText:string;Digits:Integer):string;
96var
97 I: Integer;
98begin
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;
117end;
118
119end.
120
Note: See TracBrowser for help on using the repository browser.