source: trunk/Packages/Common/UPrefixMultiplier.pas

Last change on this file was 204, checked in by chronos, 6 years ago
  • Modified: Use fgl unit instead of TemplateGenerics.
  • Modified: Update czech translation.
  • Modified: Updated packages.
File size: 4.0 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(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
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
74procedure Register;
75
76
77implementation
78
79procedure Register;
80begin
81 RegisterComponents('Common', [TPrefixMultiplier]);
82end;
83
84{ TPrefixMultiplier }
85
86function TPrefixMultiplier.TruncateDigits(Value: Double; Digits: Integer = 3): Double;
87var
88 II: Integer;
89 RealDigits: Integer;
90begin
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;
100end;
101
102function TPrefixMultiplier.Add(Value: Double; PrefixMultipliers: TPrefixMultiplierDef
103 ; UnitText:string; Digits: Integer): string;
104var
105 I: Integer;
106begin
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;
125end;
126
127end.
128
Note: See TracBrowser for help on using the repository browser.