source: Common/PrefixMultiplier.php

Last change on this file was 14, checked in by chronos, 3 years ago
  • Modified: Updated files to newer version.
File size: 3.6 KB
Line 
1<?php
2
3$PrefixMultipliers = array
4(
5 'Binary' => array
6 (
7 'BaseIndex' => 0,
8 'Definition' => array
9 (
10 array('', '', pow(2, 0)),
11 array('Ki', 'kibi', pow(2, 10)),
12 array('Mi', 'mebi', pow(2, 20)),
13 array('Gi', 'gibi', pow(2, 30)),
14 array('Ti', 'tebi', pow(2, 40)),
15 array('Pi', 'pebi', pow(2, 50)),
16 array('Ei', 'exbi', pow(2, 60)),
17 array('Zi', 'zebi', pow(2, 70)),
18 array('Yi', 'yobi', pow(2, 80)),
19 ),
20 ),
21 'Decimal' => array
22 (
23 'BaseIndex' => 8,
24 'Definition' => array
25 (
26 array('y', 'yocto', pow(10, -24)),
27 array('z', 'zepto', pow(10, -21)),
28 array('a', 'atto', pow(10, -18)),
29 array('f', 'femto', pow(10, -15)),
30 array('p', 'piko', pow(10, -12)),
31 array('n', 'nano', pow(10, -9)),
32 array('u', 'mikro', pow(10, -6)),
33 array('m', 'mili', pow(10, -3)),
34 array('', '', pow(10, 0)),
35 array('k', 'kilo', pow(10, 3)),
36 array('M', 'mega', pow(10, 6)),
37 array('G', 'giga', pow(10, 9)),
38 array('T', 'tera', pow(10, 12)),
39 array('P', 'peta', pow(10, 15)),
40 array('E', 'exa', pow(10, 18)),
41 array('Z', 'zetta', pow(10, 21)),
42 array('Y', 'yotta', pow(10, 24)),
43 ),
44 ),
45 'Time' => array
46 (
47 'BaseIndex' => 8,
48 'Definition' => array
49 (
50 array('ys', 'yoctosekunda', pow(10, -24)),
51 array('zs', 'zeptosekunda', pow(10, -21)),
52 array('as', 'attosekunda', pow(10, -18)),
53 array('fs', 'femtosekunda', pow(10, -15)),
54 array('ps', 'pikosekunda', pow(10, -12)),
55 array('ns', 'nanosekunda', pow(10, -9)),
56 array('us', 'mikrosekunda', pow(10, -6)),
57 array('ms', 'milisekunda', pow(10, -3)),
58 array('s', 'sekund', 1),
59 array('minut', 'minuta', 60),
60 array('hodin', 'hodina', 60 * 60) ,
61 array('dnů', 'den', 24 * 60 * 60),
62 array('týdnů', 'týden', 7 * 24 * 60 * 60),
63 array('měsíců', 'měsíc', 30 * 24 * 60 * 60),
64 array('roků', 'rok', 364 * 24 * 60 * 60),
65 array('desitiletí', 'desetiletí', 10 * 364 * 24 * 60 * 60),
66 array('staletí', 'staletí', 100 * 364 * 24 * 60 * 60),
67 array('tisíciletí', 'tisiciletí', 10000 * 364 * 24 * 60 * 60),
68 ),
69 ),
70);
71
72class PrefixMultiplier
73{
74 function TruncateDigits($Value, $Digits = 4): string
75 {
76 for ($II = 2; $II > -6; $II--)
77 {
78 if ($Value >= pow(10, $II))
79 {
80 if ($Digits < ($II + 1)) $RealDigits = $II + 1;
81 else $RealDigits = $Digits;
82 $Value = round($Value / pow(10, $II - $RealDigits + 1)) * pow(10, $II - $RealDigits + 1);
83 break;
84 }
85 }
86 return $Value;
87 }
88
89 function Add($Value, $Unit, $Digits = 4, $PrefixType = 'Decimal'): string
90 {
91 global $PrefixMultipliers;
92
93 $Negative = ($Value < 0);
94 $Value = abs($Value);
95 if (($Unit == '') and ($PrefixType != 'Time'))
96 return $this->TruncateDigits($Value, $Digits);
97
98 $I = $PrefixMultipliers[$PrefixType]['BaseIndex'];
99 if ($Value == 0) return $Value.' '.$PrefixMultipliers[$PrefixType]['Definition'][$I][0].$Unit;
100
101 if ($Value > 1)
102 {
103 while ((($I + 1) <= count($PrefixMultipliers[$PrefixType]['Definition'])) and (($Value / $PrefixMultipliers[$PrefixType]['Definition'][$I + 1][2]) > 1))
104 $I = $I + 1;
105 } else
106 if ($Value < 1)
107 {
108 while ((($I - 1) >= 0) and (($Value / $PrefixMultipliers[$PrefixType]['Definition'][$I][2]) < 1))
109 $I = $I - 1;
110 }
111 $Value = $Value / $PrefixMultipliers[$PrefixType]['Definition'][$I][2];
112
113 // Truncate digits count
114 $Value = $this->TruncateDigits($Value, $Digits);
115 if ($Negative) $Value = -$Value;
116 return $Value.' '.$PrefixMultipliers[$PrefixType]['Definition'][$I][0].$Unit;
117 }
118}
Note: See TracBrowser for help on using the repository browser.