source: trunk/Packages/Common/PrefixMultiplier.php

Last change on this file was 41, checked in by chronos, 4 years ago
  • Modified: Translate prefix multipliers.
  • Modified: Improve length, duration and speed calculation in detailed and daily tables.
  • Added: Allow to define lap length na money per Km for each year.
File size: 3.7 KB
Line 
1<?php
2
3function InitPrefixMultipliers()
4{
5 global $PrefixMultipliers;
6$PrefixMultipliers = array
7(
8 'Binary' => array
9 (
10 'BaseIndex' => 0,
11 'Definition' => array
12 (
13 array('', '', pow(2, 0)),
14 array('Ki', 'kibi', pow(2, 10)),
15 array('Mi', 'mebi', pow(2, 20)),
16 array('Gi', 'gibi', pow(2, 30)),
17 array('Ti', 'tebi', pow(2, 40)),
18 array('Pi', 'pebi', pow(2, 50)),
19 array('Ei', 'exbi', pow(2, 60)),
20 array('Zi', 'zebi', pow(2, 70)),
21 array('Yi', 'yobi', pow(2, 80)),
22 ),
23 ),
24 'Decimal' => array
25 (
26 'BaseIndex' => 8,
27 'Definition' => array
28 (
29 array('y', 'yocto', pow(10, -24)),
30 array('z', 'zepto', pow(10, -21)),
31 array('a', 'atto', pow(10, -18)),
32 array('f', 'femto', pow(10, -15)),
33 array('p', 'piko', pow(10, -12)),
34 array('n', 'nano', pow(10, -9)),
35 array('u', 'mikro', pow(10, -6)),
36 array('m', 'mili', pow(10, -3)),
37 array('', '', pow(10, 0)),
38 array('k', 'kilo', pow(10, 3)),
39 array('M', 'mega', pow(10, 6)),
40 array('G', 'giga', pow(10, 9)),
41 array('T', 'tera', pow(10, 12)),
42 array('P', 'peta', pow(10, 15)),
43 array('E', 'exa', pow(10, 18)),
44 array('Z', 'zetta', pow(10, 21)),
45 array('Y', 'yotta', pow(10, 24)),
46 ),
47 ),
48 'Time' => array
49 (
50 'BaseIndex' => 8,
51 'Definition' => array
52 (
53 array('ys', T('yoctosecond'), pow(10, -24)),
54 array('zs', T('zeptosecond'), pow(10, -21)),
55 array('as', T('attosecond'), pow(10, -18)),
56 array('fs', T('femtosecond'), pow(10, -15)),
57 array('ps', T('pikosecond'), pow(10, -12)),
58 array('ns', T('nanosecond'), pow(10, -9)),
59 array('us', T('microsecond'), pow(10, -6)),
60 array('ms', T('millisecond'), pow(10, -3)),
61 array('s', T('second'), 1),
62 array(T('minutes'), T('minute'), 60),
63 array(T('hours'), T('hour'), 60 * 60) ,
64 array(T('days'), T('day'), 24 * 60 * 60),
65 array(T('weeks'), T('week'), 7 * 24 * 60 * 60),
66 array(T('months'), T('month'), 30 * 24 * 60 * 60),
67 array(T('years'), T('year'), 364 * 24 * 60 * 60),
68 array(T('decades'), T('decade'), 10 * 364 * 24 * 60 * 60),
69 array(T('centuries'), T('century'), 100 * 364 * 24 * 60 * 60),
70 array(T('millennia'), T('millennium'), 10000 * 364 * 24 * 60 * 60),
71 ),
72 ),
73);
74}
75
76class PrefixMultiplier
77{
78 function TruncateDigits($Value, $Digits = 4)
79 {
80 for ($II = 2; $II > -6; $II--)
81 {
82 if ($Value >= pow(10, $II))
83 {
84 if ($Digits < ($II + 1)) $RealDigits = $II + 1;
85 else $RealDigits = $Digits;
86 $Value = round($Value / pow(10, $II - $RealDigits + 1)) * pow(10, $II - $RealDigits + 1);
87 break;
88 }
89 }
90 return $Value;
91 }
92
93 function Add($Value, $Unit, $Digits = 4, $PrefixType = 'Decimal')
94 {
95 global $PrefixMultipliers;
96
97 $Negative = ($Value < 0);
98 $Value = abs($Value);
99 if (($Unit == '') and ($PrefixType != 'Time'))
100 return $this->TruncateDigits($Value, $Digits);
101
102 $I = $PrefixMultipliers[$PrefixType]['BaseIndex'];
103 if ($Value == 0) return $Value.' '.$PrefixMultipliers[$PrefixType]['Definition'][$I][0].$Unit;
104
105 if ($Value > 1)
106 {
107 while ((($I + 1) <= count($PrefixMultipliers[$PrefixType]['Definition'])) and (($Value / $PrefixMultipliers[$PrefixType]['Definition'][$I + 1][2]) > 1))
108 $I = $I + 1;
109 } else
110 if ($Value < 1)
111 {
112 while ((($I - 1) >= 0) and (($Value / $PrefixMultipliers[$PrefixType]['Definition'][$I][2]) < 1))
113 $I = $I - 1;
114 }
115 $Value = $Value / $PrefixMultipliers[$PrefixType]['Definition'][$I][2];
116
117 // Truncate digits count
118 $Value = $this->TruncateDigits($Value, $Digits);
119 if ($Negative) $Value = -$Value;
120 return $Value.' '.$PrefixMultipliers[$PrefixType]['Definition'][$I][0].$Unit;
121 }
122}
123
124InitPrefixMultipliers();
Note: See TracBrowser for help on using the repository browser.