author | Stefan Vogel <sv@exept.de> |
Mon, 20 Feb 2017 10:41:32 +0100 | |
changeset 4337 | 167d0c973942 |
parent 4092 | 61384ffc7271 |
child 4514 | 360a740542a7 |
child 4769 | 89914ccfcf7d |
permissions | -rw-r--r-- |
4092 | 1 |
"{ Encoding: utf8 }" |
2 |
||
884 | 3 |
" |
4 |
COPYRIGHT (c) 1996 eXept Software AG |
|
5 |
All Rights Reserved |
|
6 |
||
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
981 | 14 |
"{ Package: 'stx:libbasic2' }" |
15 |
||
3486 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
363 | 18 |
Object subclass:#UnitConverter |
19 |
instanceVariableNames:'' |
|
516 | 20 |
classVariableNames:'Conversions Scaling Aliases Constants' |
363 | 21 |
poolDictionaries:'' |
22 |
category:'Magnitude-General' |
|
23 |
! |
|
24 |
||
25 |
!UnitConverter class methodsFor:'documentation'! |
|
26 |
||
884 | 27 |
copyright |
28 |
" |
|
29 |
COPYRIGHT (c) 1996 eXept Software AG |
|
30 |
All Rights Reserved |
|
31 |
||
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
||
40 |
||
41 |
! |
|
42 |
||
363 | 43 |
documentation |
44 |
" |
|
45 |
In order to collect various unit conversions into one central |
|
46 |
manageable (and especially: browsable) place, all previously |
|
564 | 47 |
spread knowledge about metric (and other) conversions has been |
363 | 48 |
brought into this class. |
49 |
||
50 |
This class is purely functional, abstract and has no instances; |
|
51 |
all queries are via class protocol. |
|
52 |
Choosing the Magnitudes category as its home is arbitrary. |
|
53 |
||
515 | 54 |
The supported units are setup in the classes initialize method; |
364 | 55 |
supported are: |
886 | 56 |
meter - inch - feet - foot - yard |
57 |
meter2 - acre - 'german-ar' 'german-hektar' are |
|
58 |
liter - gallon - barrel - floz |
|
59 |
celsius - fahrenheit |
|
364 | 60 |
and many others. |
61 |
||
515 | 62 |
The converter does a recursive search for a conversion; |
63 |
thus, if there is a conversion from #inch to #millimeter and another |
|
64 |
from #millimeter to #kilometer, conversion from #inch to #kilometer |
|
65 |
is found automatically. |
|
66 |
||
886 | 67 |
No Warranty: |
68 |
The numbers and conversion factors were obtained from the Unix |
|
69 |
units command. Please check before using it - there might be |
|
70 |
typos or wrong conversions in the setup code. |
|
71 |
||
72 |
||
1323 | 73 |
Notice: |
74 |
This class is *much* older than the younger and more object oriented |
|
75 |
Measurements package. Other than solving similar problems, these two packages |
|
76 |
are not related. |
|
77 |
||
78 |
||
363 | 79 |
[author:] |
886 | 80 |
Claus Gittinger |
363 | 81 |
|
82 |
see also: |
|
886 | 83 |
examples |
84 |
/usr/share/lib/unittab (if present on your system) |
|
1323 | 85 |
Measurement Unit |
363 | 86 |
" |
87 |
! |
|
88 |
||
89 |
examples |
|
90 |
" |
|
91 |
ever wanted to know, how many floz's are there in a european |
|
92 |
Coce bottle ? |
|
982 | 93 |
[exBegin] |
94 |
Transcript showCR: |
|
95 |
(UnitConverter convert:1 from:#liter to:#floz) |
|
96 |
[exEnd] |
|
97 |
||
98 |
[exBegin] |
|
99 |
Transcript showCR: |
|
100 |
(UnitConverter convert:43.5 from:#oz to:#gram) |
|
101 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
102 |
|
363 | 103 |
or, how many square-meters an acre is: |
982 | 104 |
[exBegin] |
105 |
Transcript showCR: |
|
106 |
(UnitConverter convert:1 from:#acre to:#'meter^2') |
|
107 |
[exEnd] |
|
363 | 108 |
|
516 | 109 |
|
515 | 110 |
or europeans might want to know, what those |
111 |
fahrenheit numbers mean in an US weather report ;-): |
|
982 | 112 |
[exBegin] |
113 |
Transcript showCR:( |
|
114 |
(UnitConverter convert:80 from:#fahrenheit to:#celsius) |
|
115 |
asFixedPoint:1) |
|
116 |
[exEnd] |
|
515 | 117 |
|
516 | 118 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
119 |
how fast do I drive ? :-) |
982 | 120 |
[exBegin] |
121 |
Transcript showCR:( |
|
122 |
UnitConverter convert:200 from:#'km/hr' to:#'mile/hr') |
|
123 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
124 |
how fast does Chris drive ? :-) |
982 | 125 |
[exBegin] |
126 |
Transcript showCR:( |
|
127 |
UnitConverter convert:65 from:#'mile/hr' to:#'km/hr') |
|
128 |
[exEnd] |
|
516 | 129 |
|
130 |
||
131 |
calories or joule ? |
|
982 | 132 |
[exBegin] |
133 |
Transcript showCR:( |
|
134 |
UnitConverter convert:0.18 from:#'kilocalorie' to:#'kilojoule') |
|
135 |
[exEnd] |
|
136 |
[exBegin] |
|
137 |
Transcript showCR:( |
|
138 |
UnitConverter convert:2000 from:#'kilocalorie' to:#'kilojoule') |
|
139 |
[exEnd] |
|
516 | 140 |
|
141 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
142 |
distances: |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
143 |
|
982 | 144 |
[exBegin] |
145 |
Transcript showCR:( |
|
146 |
UnitConverter convert:1 from:#'lightsecond' to:#'kilometer') |
|
147 |
[exEnd] |
|
4092 | 148 |
that's the same: |
982 | 149 |
[exBegin] |
150 |
Transcript showCR:( |
|
151 |
UnitConverter convert:1 from:#'lightspeed*s' to:#'kilometer') |
|
152 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
153 |
a days travel ... |
982 | 154 |
[exBegin] |
155 |
Transcript showCR:( |
|
156 |
UnitConverter convert:1 from:#'lightspeed*dy' to:#'kilometer') |
|
157 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
158 |
a year travel ... |
982 | 159 |
[exBegin] |
160 |
Transcript showCR:( |
|
161 |
UnitConverter convert:1 from:#'lightyear' to:#'kilometer') |
|
162 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
163 |
|
2320 | 164 |
wonna race on the autobahn ? |
165 |
[exBegin] |
|
166 |
Transcript showCR:( |
|
167 |
UnitConverter convert:200 from:#'km/h' to:#'mph') |
|
168 |
[exEnd] |
|
2547 | 169 |
[exBegin] |
170 |
Transcript showCR:( |
|
171 |
UnitConverter convert:13 from:#'m/s' to:#'km/h') |
|
172 |
[exEnd] |
|
2320 | 173 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
174 |
|
363 | 175 |
real estate buyers might want to know, how many acres |
176 |
a german ar is: |
|
982 | 177 |
[exBegin] |
178 |
Transcript showCR: |
|
179 |
(UnitConverter convert:1 from:#'german-ar' to:#acre) |
|
180 |
[exEnd] |
|
516 | 181 |
- or how many square-feet are there in your living room: |
982 | 182 |
[exBegin] |
183 |
Transcript showCR: |
|
184 |
(UnitConverter convert:100 from:#'meter^2' to:#'foot^2') |
|
185 |
[exEnd] |
|
1031 | 186 |
- or how about: |
187 |
[exBegin] |
|
188 |
Transcript showCR: |
|
189 |
(UnitConverter convert:3600 from:#'ft*ft' to:#'m*,') |
|
190 |
[exEnd] |
|
191 |
||
516 | 192 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
193 |
how many tea spoons are there in a cubic meter ? |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
194 |
(roughly, since a teaspoon is not a standard unit) |
982 | 195 |
[exBegin] |
196 |
Transcript showCR: |
|
197 |
(UnitConverter convert:1 from:#'meter^3' to:#teaspoon) |
|
198 |
[exEnd] |
|
365 | 199 |
how wide is a US page in inches: |
982 | 200 |
[exBegin] |
201 |
Transcript showCR: |
|
202 |
(UnitConverter convert:1 from:#'letterW' to:#inch) |
|
203 |
[exEnd] |
|
516 | 204 |
- in millimeter: |
982 | 205 |
[exBegin] |
206 |
Transcript showCR: |
|
207 |
(UnitConverter convert:1 from:#'letterW' to:#mm) |
|
208 |
[exEnd] |
|
516 | 209 |
|
210 |
||
365 | 211 |
the height of a US page in inches: |
982 | 212 |
[exBegin] |
213 |
Transcript showCR: |
|
214 |
(UnitConverter convert:1 from:#'letterH' to:#inch) |
|
215 |
[exEnd] |
|
516 | 216 |
- in millimeter: |
982 | 217 |
[exBegin] |
218 |
Transcript showCR: |
|
219 |
(UnitConverter convert:1 from:#'letterH' to:#mm) |
|
220 |
[exEnd] |
|
516 | 221 |
|
222 |
||
365 | 223 |
the same for european A4 standard page: |
982 | 224 |
[exBegin] |
225 |
Transcript showCR: |
|
226 |
(UnitConverter convert:1 from:#'a4H' to:#mm) |
|
227 |
[exEnd] |
|
228 |
[exBegin] |
|
229 |
Transcript showCR: |
|
230 |
(UnitConverter convert:1 from:#'a4H' to:#inch) |
|
231 |
[exEnd] |
|
2443 | 232 |
|
233 |
the mass of a proton: |
|
234 |
[exBegin] |
|
235 |
Transcript showCR: |
|
236 |
(UnitConverter convert:1 from:#'proton' to:#eV) |
|
237 |
[exEnd] |
|
238 |
the energy of a single proton in the LHC (as of 2010) |
|
239 |
[exBegin] |
|
240 |
Transcript showCR: |
|
241 |
(UnitConverter convert:7 from:#'TeV' to:#joule) |
|
242 |
[exEnd] |
|
363 | 243 |
" |
244 |
! ! |
|
245 |
||
246 |
!UnitConverter class methodsFor:'initialization'! |
|
247 |
||
550 | 248 |
initializeConversions |
363 | 249 |
"initialize common conversions" |
250 |
||
1135 | 251 |
self initializeScaleFactors. |
252 |
||
363 | 253 |
Conversions := IdentityDictionary new. |
254 |
Aliases := IdentityDictionary new. |
|
516 | 255 |
Constants := IdentityDictionary new. |
363 | 256 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
257 |
"/ ---------- velocity ------------- |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
258 |
|
556 | 259 |
Constants at:#lightspeed put:#(2.997925E8 #'m/s'). |
2443 | 260 |
Constants at:#soundspeed put:#(343 #'m/s'). "/ 20degC - AIR |
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
261 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
262 |
|
363 | 263 |
"/ -------------- length ------------- |
264 |
||
265 |
Aliases at:#km put:#kilometer. |
|
266 |
Aliases at:#m put:#meter. |
|
267 |
Aliases at:#dm put:#decimeter. |
|
268 |
Aliases at:#cm put:#centimeter. |
|
269 |
Aliases at:#mm put:#millimeter. |
|
270 |
Aliases at:#micron put:#micrometer. |
|
271 |
Aliases at:#nm put:#nanometer. |
|
272 |
Aliases at:#angstrom put:#decinanometer. |
|
273 |
||
274 |
"/ US |
|
275 |
self addConversion:12 from:#foot to:#inch. |
|
276 |
self addConversion:3 from:#yard to:#foot. |
|
277 |
self addConversion:5280 from:#mile to:#foot. |
|
515 | 278 |
self addConversion:0.001 from:#inch to:#mil. |
363 | 279 |
|
2320 | 280 |
Aliases at:#feet put:#foot. |
281 |
Aliases at:#ft put:#foot. |
|
282 |
Aliases at:#yd put:#yard. |
|
283 |
Aliases at:#mi put:#mile. |
|
284 |
Aliases at:#mph put:#'mile/hr'. |
|
363 | 285 |
|
286 |
"/ inch to millimeter |
|
287 |
self addConversion:(25.4/1000) from:#inch to:#meter. |
|
288 |
||
365 | 289 |
"/ nautic |
290 |
self addConversion:1852 from:#'nautical-mile' to:#meter. |
|
291 |
||
556 | 292 |
Aliases at:#lightsecond put:#'lightspeed*s'. |
293 |
Aliases at:#lightyear put:#'lightspeed*yr'. |
|
294 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
295 |
self addConversion:((Constants at:#lightspeed) at:1) from:#'lightspeed*s' to:#'meter'. |
385 | 296 |
|
516 | 297 |
"/ ---------- time ------------- |
298 |
||
299 |
self addConversion:60 from:#min to:#s. |
|
300 |
self addConversion:60 from:#hr to:#min. |
|
301 |
self addConversion:24 from:#dy to:#hr. |
|
302 |
self addConversion:365.24219879 from:#yr to:#dy. |
|
303 |
||
2320 | 304 |
Aliases at:#h put:#'hr'. |
305 |
Aliases at:#d put:#'dy'. |
|
516 | 306 |
|
385 | 307 |
"/ ---------- printing ------------- |
1135 | 308 |
self initializePrintValues. |
365 | 309 |
|
1135 | 310 |
"/ ---------------- area ------------------- |
311 |
||
312 |
"/ US |
|
313 |
self addConversion:4840 from:#acre to:#'yard^2'. |
|
314 |
||
315 |
self addConversion:100 from:#are to:#'meter^2'. |
|
316 |
self addConversion:100 from:#hectare to:#are. |
|
317 |
||
3701 | 318 |
"/ german area - add your countries, and return to me ... |
1135 | 319 |
Aliases at:#'german-ar' put:#are. |
320 |
Aliases at:#'german-hektar' put:#hectare. |
|
321 |
||
322 |
||
323 |
"/ ---------------- liquid ---------------- |
|
324 |
||
325 |
self addConversion:231 from:#gallon to:#'inch^3'. |
|
326 |
self addConversion:(1/4) from:#quart to:#gallon. "/ well - at least here, |
|
4092 | 327 |
"/ that's also 1/4th of a good wine ;-) |
1135 | 328 |
self addConversion:(1/2) from:#pint to:#quart. |
329 |
self addConversion:(1/16) from:#floz to:#pint. |
|
330 |
self addConversion:(1/8) from:#fldr to:#floz. |
|
331 |
||
332 |
self addConversion:42 from:#barrel to:#gallon. |
|
333 |
self addConversion:35.23907 from:#bushel to:#liter. |
|
334 |
||
335 |
Aliases at:#cc put:#'cm^3'. |
|
336 |
Aliases at:#liter put:#kilocc. |
|
337 |
Aliases at:#ml put:#milliliter. |
|
338 |
Aliases at:#gal put:#gallon. |
|
339 |
Aliases at:#qt put:#quart. |
|
340 |
||
2320 | 341 |
"/ well, a pint of beer is not always: |
342 |
self addConversion:277.420 from:#'british-gallon' to:#'inch^3'. |
|
343 |
self addConversion:(1/4) from:#'british-quart' to:#'british-gallon'. |
|
344 |
self addConversion:(1/2) from:#'british-pint' to:#'british-quart'. |
|
345 |
self addConversion:(1/16) from:#'british-floz' to:#'british-pint'. |
|
1135 | 346 |
|
347 |
"/ ---------------- mass ---------------- |
|
348 |
||
349 |
self addConversion:28.35 from:#ounce to:#gram. |
|
350 |
self addConversion:453.59237 from:#lb to:#gram. |
|
351 |
self addConversion:205 from:#carat to:#milligram. |
|
352 |
||
2444
6c4c6addc97a
changed: #initializeConversions
Claus Gittinger <cg@exept.de>
parents:
2443
diff
changeset
|
353 |
self addConversion:0.938272013 from:#proton to:#GeV. |
6c4c6addc97a
changed: #initializeConversions
Claus Gittinger <cg@exept.de>
parents:
2443
diff
changeset
|
354 |
self addConversion:0.510998910 from:#electron to:#MeV. |
6c4c6addc97a
changed: #initializeConversions
Claus Gittinger <cg@exept.de>
parents:
2443
diff
changeset
|
355 |
self addConversion:0.939565346 from:#neutron to:#GeV. |
2443 | 356 |
|
1135 | 357 |
Aliases at:#oz put:#ounce. |
358 |
Aliases at:#lbs put:#lb. |
|
359 |
||
360 |
Aliases at:#gm put:#gram. |
|
361 |
Aliases at:#kg put:#kilogram. |
|
362 |
||
363 |
"/ ---------------- energy ---------------- |
|
364 |
||
365 |
Aliases at:#cal put:#calorie. |
|
366 |
Aliases at:#nt put:#newton. |
|
367 |
Aliases at:#joule put:#'nt*m'. |
|
2320 | 368 |
Aliases at:#J put:#'joule'. |
369 |
Aliases at:#N put:#'newton'. |
|
2443 | 370 |
Aliases at:#eV put:#'electronvolt'. |
371 |
Aliases at:#MeV put:#'megaeV'. |
|
372 |
Aliases at:#GeV put:#'gigaeV'. |
|
373 |
Aliases at:#TeV put:#'teraeV'. |
|
2320 | 374 |
|
375 |
Aliases at:#watt put:#'J/s'. |
|
1135 | 376 |
|
377 |
self addConversion:4.1868 from:#calorie to:#joule. |
|
2443 | 378 |
self addConversion:1.60217653E-19 from:#electronvolt to:#joule. |
1135 | 379 |
|
380 |
||
381 |
"/ ---------------- cooking ---------------- |
|
382 |
||
383 |
self addConversion:4 from:#tablespoon to:#fldr. |
|
384 |
self addConversion:(4/3) from:#teaspoon to:#fldr. |
|
385 |
||
386 |
||
387 |
"/ ---------------- temperature ---------------- |
|
388 |
||
389 |
self addConversion:[:d | d * 1.8 + 32] from:#celsius to:#fahrenheit. |
|
2443 | 390 |
self addConversion:[:f | (f - 32) / 1.8] from:#fahrenheit to:#celsius. |
1135 | 391 |
|
3701 | 392 |
"/ ---------------- nature ---------------- |
393 |
Constants at:#planckLength put:#(1.616e-35 #'m'). |
|
394 |
Constants at:#planckMass put:#(2.176-8 #'Kg'). |
|
395 |
Constants at:#planckTime put:#(5.391e-44 #'s'). |
|
396 |
||
1135 | 397 |
" |
398 |
Conversions := nil. |
|
399 |
UnitConverter initializeConversions |
|
400 |
" |
|
401 |
||
402 |
"Created: / 22.7.1997 / 13:56:40 / cg" |
|
403 |
"Modified: / 10.10.2001 / 14:39:04 / cg" |
|
404 |
! |
|
405 |
||
406 |
initializePrintValues |
|
363 | 407 |
"/ inch to (roughly) a typesetter point |
408 |
self addConversion:(1/72) from:#point to:#inch. |
|
409 |
||
410 |
"/ point to twips; 20 twips (as in Rich-Text-Format) make a point |
|
411 |
self addConversion:(1/20) from:#twip to:#point. |
|
412 |
||
365 | 413 |
"/ US paper |
414 |
self addConversion:11 from:#'letter-page-height' to:#inch. |
|
415 |
self addConversion:8.5 from:#'letter-page-width' to:#inch. |
|
416 |
self addConversion:8.5 from:#'letter-landscape-page-height' to:#inch. |
|
417 |
self addConversion:11 from:#'letter-landscape-page-width' to:#inch. |
|
418 |
||
385 | 419 |
self addConversion:14 from:#'legal-page-height' to:#inch. |
420 |
self addConversion:8.5 from:#'legal-page-width' to:#inch. |
|
421 |
self addConversion:8.5 from:#'legal-landscape-page-height' to:#inch. |
|
422 |
self addConversion:14 from:#'legal-landscape-page-width' to:#inch. |
|
423 |
||
424 |
self addConversion:11 from:#'ledger-page-height' to:#inch. |
|
425 |
self addConversion:17 from:#'ledger-page-width' to:#inch. |
|
426 |
self addConversion:17 from:#'ledger-landscape-page-height' to:#inch. |
|
427 |
self addConversion:11 from:#'ledger-landscape-page-width' to:#inch. |
|
428 |
||
365 | 429 |
"/ European paper |
430 |
self addConversion:840 from:#'a1-page-height' to:#millimeter. |
|
431 |
self addConversion:592 from:#'a1-page-width' to:#millimeter. |
|
432 |
self addConversion:592 from:#'a1-landscape-page-height' to:#millimeter. |
|
433 |
self addConversion:840 from:#'a1-landscape-page-width' to:#millimeter. |
|
434 |
||
435 |
self addConversion:592 from:#'a2-page-height' to:#millimeter. |
|
436 |
self addConversion:420 from:#'a2-page-width' to:#millimeter. |
|
437 |
self addConversion:420 from:#'a2-landscape-page-height' to:#millimeter. |
|
438 |
self addConversion:592 from:#'a2-landscape-page-width' to:#millimeter. |
|
439 |
||
440 |
self addConversion:420 from:#'a3-page-height' to:#millimeter. |
|
441 |
self addConversion:296 from:#'a3-page-width' to:#millimeter. |
|
442 |
self addConversion:296 from:#'a3-landscape-page-height' to:#millimeter. |
|
443 |
self addConversion:420 from:#'a3-landscape-page-width' to:#millimeter. |
|
444 |
||
445 |
self addConversion:296 from:#'a4-page-height' to:#millimeter. |
|
446 |
self addConversion:210 from:#'a4-page-width' to:#millimeter. |
|
447 |
self addConversion:210 from:#'a4-landscape-page-height' to:#millimeter. |
|
448 |
self addConversion:296 from:#'a4-landscape-page-width' to:#millimeter. |
|
449 |
||
450 |
self addConversion:210 from:#'a5-page-height' to:#millimeter. |
|
451 |
self addConversion:148 from:#'a5-page-width' to:#millimeter. |
|
452 |
self addConversion:148 from:#'a5-landscape-page-height' to:#millimeter. |
|
453 |
self addConversion:210 from:#'a5-landscape-page-width' to:#millimeter. |
|
454 |
||
455 |
self addConversion:148 from:#'a6-page-height' to:#millimeter. |
|
456 |
self addConversion:105 from:#'a6-page-width' to:#millimeter. |
|
457 |
self addConversion:105 from:#'a6-landscape-page-height' to:#millimeter. |
|
458 |
self addConversion:148 from:#'a6-landscape-page-width' to:#millimeter. |
|
459 |
||
385 | 460 |
self addConversion:257 from:#'b5-page-height' to:#millimeter. |
461 |
self addConversion:182 from:#'b5-page-width' to:#millimeter. |
|
462 |
self addConversion:182 from:#'b5-landscape-page-height' to:#millimeter. |
|
463 |
self addConversion:257 from:#'b5-landscape-page-width' to:#millimeter. |
|
464 |
||
465 |
||
365 | 466 |
Aliases at:#letterW put:#'letter-page-width'. |
467 |
Aliases at:#letterH put:#'letter-page-height'. |
|
385 | 468 |
Aliases at:#legalW put:#'legal-page-width'. |
469 |
Aliases at:#legalH put:#'legal-page-height'. |
|
470 |
Aliases at:#ledgerW put:#'ledger-page-width'. |
|
471 |
Aliases at:#ledgerH put:#'ledger-page-height'. |
|
365 | 472 |
Aliases at:#a1W put:#'a1-page-width'. |
473 |
Aliases at:#a1H put:#'a1-page-height'. |
|
474 |
Aliases at:#a2W put:#'a2-page-width'. |
|
475 |
Aliases at:#a2H put:#'a2-page-height'. |
|
476 |
Aliases at:#a3W put:#'a3-page-width'. |
|
477 |
Aliases at:#a3H put:#'a3-page-height'. |
|
478 |
Aliases at:#a4W put:#'a4-page-width'. |
|
479 |
Aliases at:#a4H put:#'a4-page-height'. |
|
480 |
Aliases at:#a5W put:#'a5-page-width'. |
|
481 |
Aliases at:#a5H put:#'a5-page-height'. |
|
482 |
Aliases at:#a6W put:#'a6-page-width'. |
|
483 |
Aliases at:#a6H put:#'a6-page-height'. |
|
385 | 484 |
Aliases at:#b5W put:#'b5-page-width'. |
485 |
Aliases at:#b5H put:#'b5-page-height'. |
|
365 | 486 |
|
487 |
Aliases at:#letterlW put:#'letter-landscape-page-width'. |
|
488 |
Aliases at:#letterlH put:#'letter-landscape-page-height'. |
|
385 | 489 |
Aliases at:#legallW put:#'legal-landscape-page-width'. |
490 |
Aliases at:#legallH put:#'legal-landscape-page-height'. |
|
491 |
Aliases at:#ledgerlW put:#'ledger-landscape-page-width'. |
|
492 |
Aliases at:#ledgerlH put:#'ledger-landscape-page-height'. |
|
365 | 493 |
Aliases at:#a1lW put:#'a1-landscape-page-width'. |
494 |
Aliases at:#a1lH put:#'a1-landscape-page-height'. |
|
495 |
Aliases at:#a2lW put:#'a2-landscape-page-width'. |
|
496 |
Aliases at:#a2lH put:#'a2-landscape-page-height'. |
|
497 |
Aliases at:#a3lW put:#'a3-landscape-page-width'. |
|
498 |
Aliases at:#a3lH put:#'a3-landscape-page-height'. |
|
499 |
Aliases at:#a4lW put:#'a4-landscape-page-width'. |
|
500 |
Aliases at:#a4lH put:#'a4-landscape-page-height'. |
|
501 |
Aliases at:#a5lW put:#'a5-landscape-page-width'. |
|
502 |
Aliases at:#a5lH put:#'a5-landscape-page-height'. |
|
503 |
Aliases at:#a6lW put:#'a6-landscape-page-width'. |
|
504 |
Aliases at:#a6lH put:#'a6-landscape-page-height'. |
|
385 | 505 |
Aliases at:#b5lW put:#'b5-landscape-page-width'. |
506 |
Aliases at:#b5lH put:#'b5-landscape-page-height'. |
|
1135 | 507 |
! |
363 | 508 |
|
1135 | 509 |
initializeScaleFactors |
510 |
Scaling := IdentityDictionary new. |
|
363 | 511 |
|
1285 | 512 |
Scaling at:#yotta put:1000000000000000000000000. "/ 10^24 |
513 |
Scaling at:#zetta put:1000000000000000000000. "/ 10^21 |
|
514 |
Scaling at:#exa put:1000000000000000000. "/ 10^18 |
|
515 |
Scaling at:#peta put:1000000000000000. "/ 10^15 |
|
516 |
Scaling at:#tera put:1000000000000. "/ 10^12 |
|
517 |
Scaling at:#giga put:1000000000. "/ 10^9 |
|
518 |
Scaling at:#mega put:1000000. "/ 10^6 |
|
1135 | 519 |
Scaling at:#myria put:10000. |
1285 | 520 |
Scaling at:#kilo put:1000. "/ 10^3 |
521 |
Scaling at:#hecto put:100. "/ 10^2 |
|
522 |
Scaling at:#deca put:10. "/ 10^1 |
|
2320 | 523 |
Scaling at:#deka put:10. "/ 10^1 |
1323 | 524 |
Scaling at:#deci put:(1/10). "/ 10^-1 |
1135 | 525 |
Scaling at:#centi put:(1/100). |
1323 | 526 |
Scaling at:#milli put:(1/1000). "/ 10^-3 |
527 |
Scaling at:#micro put:(1/1000000). "/ 10^-6 |
|
528 |
Scaling at:#nano put:(1/1000000000). "/ 10^-9 |
|
529 |
Scaling at:#pico put:(1/1000000000000). "/ 10^-12 |
|
530 |
Scaling at:#femto put:(1/1000000000000000). "/ 10^-15 |
|
531 |
Scaling at:#atto put:(1/1000000000000000000). "/ 10^-18 |
|
532 |
Scaling at:#zepto put:(1/1000000000000000000000). "/ 10^-21 |
|
533 |
Scaling at:#yocto put:(1/1000000000000000000000000)."/ 10^-24 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
534 |
! ! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
535 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
536 |
!UnitConverter class methodsFor:'accessing'! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
537 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
538 |
scalings |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
539 |
"return the set of known scaling prefixes" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
540 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
541 |
^ Scaling keys |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
542 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
543 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
544 |
UnitConverter scalings |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
545 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
546 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
547 |
"Created: 6.8.1997 / 16:53:19 / cg" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
548 |
"Modified: 6.8.1997 / 16:58:11 / cg" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
549 |
! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
550 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
551 |
units |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
552 |
"return the set of known units" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
553 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
554 |
|setOfUnits| |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
555 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
556 |
setOfUnits := IdentitySet new. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
557 |
Conversions keysAndValuesDo:[:srcUnit :conversionInfo | |
601 | 558 |
setOfUnits add:srcUnit. |
559 |
conversionInfo keysDo:[:targetUnit | |
|
560 |
setOfUnits add:targetUnit |
|
561 |
] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
562 |
]. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
563 |
^ setOfUnits |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
564 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
565 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
566 |
UnitConverter units |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
567 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
568 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
569 |
"Created: 6.8.1997 / 16:57:08 / cg" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
570 |
"Modified: 6.8.1997 / 16:57:47 / cg" |
363 | 571 |
! ! |
572 |
||
573 |
!UnitConverter class methodsFor:'conversions'! |
|
574 |
||
575 |
convert:howMany from:sourceUnit to:destUnit |
|
576 |
"given a value in sourceUnit (symbolic), try to convert it |
|
577 |
to destUnit (symbolic); return nil, if the conversion is |
|
578 |
unknown." |
|
579 |
||
516 | 580 |
|u conversions alias rslt sU dU const val unit |
581 |
i suNumerator suDenominator duNumerator duDenominator uN uD |
|
582 |
sF1 sF2 dF1 dF2 |
|
583 |
s d| |
|
363 | 584 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
585 |
"/ Transcript showCR:('try ' , sourceUnit , '->' , destUnit). |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
586 |
|
550 | 587 |
Conversions isNil ifTrue:[ |
1031 | 588 |
self initializeConversions |
550 | 589 |
]. |
590 |
||
363 | 591 |
"/ somehow, recursion must end ... |
592 |
sourceUnit == destUnit ifTrue:[ |
|
1031 | 593 |
^ howMany |
363 | 594 |
]. |
595 |
||
369 | 596 |
sourceUnit isSymbol ifFalse:[ |
1031 | 597 |
s := sourceUnit withoutSeparators. |
598 |
sU := s asSymbolIfInterned. |
|
599 |
sU notNil ifTrue:[ |
|
600 |
^ self convert:howMany from:sU to:destUnit |
|
601 |
]. |
|
602 |
(s startsWith:$() ifTrue:[ |
|
603 |
(s endsWith:$)) ifTrue:[ |
|
604 |
s := s copyFrom:2 to:(s size - 1). |
|
605 |
^ self convert:howMany from:s to:destUnit |
|
606 |
] |
|
607 |
]. |
|
369 | 608 |
]. |
609 |
destUnit isSymbol ifFalse:[ |
|
1031 | 610 |
d := destUnit withoutSeparators. |
611 |
dU := d asSymbolIfInterned. |
|
612 |
dU notNil ifTrue:[ |
|
613 |
^ self convert:howMany from:sourceUnit to:dU |
|
614 |
]. |
|
615 |
(d startsWith:$() ifTrue:[ |
|
616 |
(d endsWith:$)) ifTrue:[ |
|
617 |
d := d copyFrom:2 to:(d size - 1). |
|
618 |
^ self convert:howMany from:sourceUnit to:d |
|
619 |
] |
|
620 |
]. |
|
369 | 621 |
]. |
622 |
||
363 | 623 |
"/ first, get rid of scalers ... |
624 |
||
625 |
u := self unscaled:sourceUnit. |
|
626 |
u notNil ifTrue:[ |
|
1031 | 627 |
^ self convert:(howMany*(u value)) from:(u key) to:destUnit |
363 | 628 |
]. |
629 |
||
630 |
u := self unscaled:destUnit. |
|
631 |
u notNil ifTrue:[ |
|
1031 | 632 |
^ self convert:(howMany/(u value)) from:sourceUnit to:(u key) |
363 | 633 |
]. |
634 |
||
635 |
"/ and of aliases ... |
|
636 |
||
637 |
alias := Aliases at:sourceUnit ifAbsent:nil. |
|
638 |
alias notNil ifTrue:[ |
|
1031 | 639 |
^ self convert:howMany from:alias to:destUnit |
363 | 640 |
]. |
641 |
alias := Aliases at:destUnit ifAbsent:nil. |
|
642 |
alias notNil ifTrue:[ |
|
1031 | 643 |
^ self convert:howMany from:sourceUnit to:alias |
363 | 644 |
]. |
645 |
||
516 | 646 |
"/ any constants ? |
647 |
||
648 |
(Constants includesKey:sourceUnit) ifTrue:[ |
|
1031 | 649 |
const := Constants at:sourceUnit. |
650 |
val := const at:1. |
|
651 |
unit := const at:2. |
|
652 |
^ self convert:(howMany*val) from:unit to:destUnit |
|
516 | 653 |
]. |
654 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
655 |
"/ compounds (^ , / or *) are very naively parsed |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
656 |
"/ need a full expression parser (tree) for full power. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
657 |
"/ I leave that as an excercise to you ... |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
658 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
659 |
|
363 | 660 |
"/ working with squares or cubics ? |
661 |
||
516 | 662 |
((sourceUnit endsWith:'^2') and:[destUnit endsWith:'^2']) ifTrue:[ |
2988
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
663 |
sU := (sourceUnit copyButLast:2) asSymbolIfInterned. |
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
664 |
dU := (destUnit copyButLast:2) asSymbolIfInterned. |
1031 | 665 |
(sU notNil and:[dU notNil]) ifTrue:[ |
666 |
^ (self convert:(howMany sqrt) from:sU to:dU) squared |
|
667 |
]. |
|
668 |
^ self noConversionFrom:sourceUnit to:destUnit. |
|
363 | 669 |
]. |
516 | 670 |
((sourceUnit endsWith:'^3') and:[destUnit endsWith:'^3']) ifTrue:[ |
2988
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
671 |
sU := (sourceUnit copyButLast:2) asSymbolIfInterned. |
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
672 |
dU := (destUnit copyButLast:2) asSymbolIfInterned. |
1031 | 673 |
(sU notNil and:[dU notNil]) ifTrue:[ |
674 |
^ (self convert:(howMany raisedTo:(1/3)) from:sU to:dU) raisedTo:3 |
|
675 |
]. |
|
676 |
^ self noConversionFrom:sourceUnit to:destUnit. |
|
516 | 677 |
]. |
678 |
||
679 |
"/ working with fractions ? |
|
680 |
||
681 |
((sourceUnit includes:$/) and:[destUnit includes:$/]) ifTrue:[ |
|
1031 | 682 |
"/ look for a constant conversion factor |
683 |
i := sourceUnit indexOf:$/. |
|
684 |
suNumerator := sourceUnit copyTo:(i - 1). |
|
685 |
suDenominator := sourceUnit copyFrom:(i + 1). |
|
686 |
i := destUnit indexOf:$/. |
|
687 |
duNumerator := destUnit copyTo:(i - 1). |
|
688 |
duDenominator := destUnit copyFrom:(i + 1). |
|
516 | 689 |
|
1031 | 690 |
uN := self convert:howMany from:suNumerator to:duNumerator. |
691 |
uN notNil ifTrue:[ |
|
692 |
uD := self convert:1 from:suDenominator to:duDenominator. |
|
693 |
uD notNil ifTrue:[ |
|
694 |
^ uN / uD |
|
695 |
] |
|
696 |
]. |
|
516 | 697 |
]. |
698 |
||
699 |
"/ working with products ? |
|
700 |
||
701 |
((sourceUnit includes:$*) and:[destUnit includes:$*]) ifTrue:[ |
|
1031 | 702 |
i := sourceUnit indexOf:$*. |
703 |
sF1 := sourceUnit copyTo:(i - 1). |
|
704 |
sF2 := sourceUnit copyFrom:(i + 1). |
|
705 |
i := destUnit indexOf:$*. |
|
706 |
dF1 := destUnit copyTo:(i - 1). |
|
707 |
dF2 := destUnit copyFrom:(i + 1). |
|
516 | 708 |
|
1031 | 709 |
u := self convert:howMany from:sF1 to:dF1. |
710 |
u notNil ifTrue:[ |
|
711 |
u := self convert:u from:sF2 to:dF2. |
|
712 |
u notNil ifTrue:[ |
|
713 |
^ u |
|
714 |
] |
|
715 |
]. |
|
363 | 716 |
]. |
717 |
||
718 |
"/ the real work comes here ... |
|
719 |
||
720 |
"/ is there a direct conversion in the dataBase ? |
|
721 |
||
722 |
rslt := self convertDirect:howMany from:sourceUnit to:destUnit. |
|
723 |
rslt notNil ifTrue:[^ rslt]. |
|
724 |
||
725 |
"/ try inverse conversion ... |
|
726 |
||
727 |
rslt := self convertDirect:1 from:destUnit to:sourceUnit. |
|
728 |
rslt notNil ifTrue:[ ^ howMany / rslt]. |
|
729 |
||
516 | 730 |
"/ here's the deep recursion ... |
363 | 731 |
|
732 |
||
733 |
"/ try indirect conversion from source |
|
734 |
||
735 |
conversions := Conversions at:sourceUnit ifAbsent:nil. |
|
736 |
conversions notNil ifTrue:[ |
|
1031 | 737 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 | |
738 |
|factor2| |
|
363 | 739 |
|
1031 | 740 |
factor2 := self convert:factor1 from:intermediateUnit to:destUnit. |
741 |
factor2 notNil ifTrue:[^ factor2 * howMany]. |
|
742 |
]. |
|
363 | 743 |
]. |
744 |
||
745 |
"/ try indirect conversion from dest |
|
746 |
||
747 |
conversions := Conversions at:destUnit ifAbsent:nil. |
|
748 |
conversions notNil ifTrue:[ |
|
1031 | 749 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 | |
750 |
|factor2| |
|
363 | 751 |
|
1031 | 752 |
factor2 := self convert:(factor1) from:intermediateUnit to:sourceUnit. |
753 |
factor2 notNil ifTrue:[^ howMany / factor2]. |
|
754 |
]. |
|
363 | 755 |
]. |
756 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
757 |
"/ if working with a product, try each component |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
758 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
759 |
(sourceUnit includes:$*) ifTrue:[ |
1031 | 760 |
i := sourceUnit indexOf:$*. |
761 |
sF1 := sourceUnit copyTo:(i - 1). |
|
762 |
sF2 := sourceUnit copyFrom:(i + 1). |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
763 |
|
1031 | 764 |
"/ see what we have ... |
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
765 |
|
1031 | 766 |
self units do:[:aUnit | |
767 |
|pref iUnit factor2 rslt| |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
768 |
|
1031 | 769 |
pref := sF1 , '*'. |
770 |
(aUnit startsWith:pref) ifTrue:[ |
|
771 |
"/ ok; want a/b -> x |
|
772 |
"/ found a/c -> any |
|
773 |
"/ what about c->b ? |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
774 |
|
1031 | 775 |
iUnit := aUnit copyFrom:pref size + 1. |
776 |
factor2 := self convert:1 from:sF2 to:iUnit. |
|
777 |
factor2 notNil ifTrue:[ |
|
778 |
"/ good ... |
|
779 |
rslt := self convert:(factor2 * howMany) |
|
780 |
from:aUnit to:destUnit. |
|
781 |
rslt notNil ifTrue:[^ rslt]. |
|
782 |
] |
|
783 |
] |
|
784 |
]. |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
785 |
]. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
786 |
|
516 | 787 |
^ self noConversionFrom:sourceUnit to:destUnit. |
363 | 788 |
|
789 |
"direct - how many meters are there in two inches: |
|
790 |
UnitConverter convert:2 from:#inch to:#meter |
|
515 | 791 |
UnitConverter convert:37 from:#degrees to:#fahrenheit |
363 | 792 |
|
793 |
reverse - how many inches are there in one meter |
|
794 |
UnitConverter convert:1 from:#meter to:#inch |
|
795 |
||
796 |
with alias: |
|
797 |
UnitConverter convert:1 from:#inch to:#m |
|
798 |
UnitConverter convert:1 from:#inch to:#mm |
|
799 |
UnitConverter convert:1 from:#inch to:#millimeter |
|
800 |
UnitConverter convert:1 from:#inch to:#nanometer |
|
801 |
UnitConverter convert:1 from:#mm to:#km |
|
802 |
UnitConverter convert:1 from:#km to:#foot |
|
803 |
||
804 |
indirect: |
|
805 |
UnitConverter convert:1 from:#mm to:#twip |
|
806 |
UnitConverter convert:1 from:#inch to:#twip |
|
369 | 807 |
UnitConverter convert:1 from:'letterH' to:#point |
808 |
UnitConverter convert:1 from:'letterlH' to:#point |
|
364 | 809 |
|
810 |
UnitConverter convert:5 from:#barrel to:#liter |
|
811 |
UnitConverter convert:10 from:#kilogram to:#carat |
|
812 |
||
813 |
UnitConverter convert:1 from:#liter to:#floz |
|
363 | 814 |
" |
815 |
||
816 |
"Created: 31.5.1996 / 16:23:38 / cg" |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
817 |
"Modified: 6.8.1997 / 18:10:22 / cg" |
363 | 818 |
! |
819 |
||
3573 | 820 |
fileSizeBinaryStringFor:nBytes |
3575 | 821 |
"return a useful string for a size-in-bytes in programmer's notation; |
3573 | 822 |
Scales by Ki (*1024), Mi (*1024*1024) or Gi (*1024*1024*1024) or even Ti |
823 |
as required and useful. |
|
824 |
Provided here since this is so common... |
|
825 |
Notice: the scale is 1024 |
|
826 |
the 1024 scale has been standardized in 1998 by IEC to MiB, GiB, etc. |
|
827 |
See https://en.wikipedia.org/wiki/Mebibyte" |
|
828 |
||
829 |
^ self |
|
830 |
unitStringFor:nBytes |
|
3574 | 831 |
scale:1024 |
3575 | 832 |
rounded:true |
3576 | 833 |
unitStrings:#('Byte' 'KiB' 'MiB' 'GiB' 'TiB' 'PiB' 'EiB' 'ZiB' 'YiB') |
3573 | 834 |
|
835 |
"/ the one below was written to save divisions. |
|
836 |
"/ It seems to be not worth the effort... |
|
837 |
||
838 |
"/ |unitString n| |
|
839 |
"/ |
|
840 |
"/ nBytes isNil ifTrue:[ |
|
841 |
"/ ^ '-' |
|
842 |
"/ ]. |
|
843 |
"/ |
|
844 |
"/ nBytes < (1000 * 1024 * 1024 * 1024) ifTrue:[ |
|
845 |
"/ nBytes < (1000 * 1024 * 1024) ifTrue:[ |
|
846 |
"/ nBytes < (1000 * 1024) ifTrue:[ |
|
847 |
"/ nBytes < 1000 ifTrue:[ |
|
848 |
"/ n := nBytes. |
|
849 |
"/ unitString := ' '. |
|
850 |
"/ ] ifFalse:[ |
|
851 |
"/ n := (nBytes / 1024) asFixedPoint:1. |
|
852 |
"/ unitString := ' K' |
|
853 |
"/ ] |
|
854 |
"/ ] ifFalse:[ |
|
855 |
"/ n := (nBytes / (1024 * 1024)) asFixedPoint:1. |
|
856 |
"/ unitString := ' M' |
|
857 |
"/ ]. |
|
858 |
"/ ] ifFalse:[ |
|
859 |
"/ n := (nBytes / 1024 / (1024 * 1024)) asFixedPoint:1. |
|
860 |
"/ unitString := ' G' |
|
861 |
"/ ] |
|
862 |
"/ ] ifFalse:[ |
|
863 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
864 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
865 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
866 |
"/ unitString := ' E' |
|
867 |
"/ ] ifFalse:[ |
|
868 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / 1024) asFixedPoint:1. |
|
869 |
"/ unitString := ' P' |
|
870 |
"/ ] |
|
871 |
"/ ] ifFalse:[ |
|
872 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
873 |
"/ unitString := ' T' |
|
874 |
"/ ]. |
|
875 |
"/ ]. |
|
876 |
"/ |
|
877 |
"/"/ n truncated = n ifTrue:[ |
|
878 |
"/"/ n := n truncated |
|
879 |
"/"/ ]. |
|
880 |
"/ ^ n printString , unitString. |
|
881 |
||
882 |
" |
|
883 |
self fileSizeStringFor:nil |
|
884 |
self fileSizeStringFor:10 |
|
885 |
self fileSizeStringFor:100 |
|
886 |
self fileSizeStringFor:1000 |
|
887 |
self fileSizeStringFor:8192 |
|
888 |
self fileSizeStringFor:10000 |
|
889 |
self fileSizeStringFor:100000 |
|
890 |
self fileSizeStringFor:1000000 |
|
891 |
self fileSizeStringFor:10000000 |
|
892 |
self fileSizeStringFor:100000000 |
|
893 |
self fileSizeStringFor:1000000000 |
|
894 |
self fileSizeStringFor:10000000000 |
|
895 |
self fileSizeStringFor:100000000000 |
|
896 |
self fileSizeStringFor:1000000000000 |
|
897 |
self fileSizeStringFor:10000000000000 |
|
898 |
self fileSizeStringFor:10000000000000000 |
|
899 |
self fileSizeStringFor:100000000000000000000 |
|
900 |
self fileSizeStringFor:1000000000000000000000 |
|
901 |
" |
|
902 |
||
903 |
"Modified: / 17-12-2013 / 16:55:17 / cg" |
|
904 |
"Modified (comment): / 18-12-2014 / 09:54:33 / sr" |
|
905 |
! |
|
906 |
||
1292 | 907 |
fileSizeFromString:fileSizeString |
3573 | 908 |
"given a fileSize string (i.e. a number followed by optional kB, MB, GB etc. or KiB, MiB, GiB,...). |
909 |
Notice that in 1998, IEC standardized the scales to 1000 for k,M,G,... and 1024 for Ki, Mi, Gi, etc. |
|
910 |
See https://en.wikipedia.org/wiki/Mebibyte. |
|
1292 | 911 |
Provided here since this is so common..." |
912 |
||
3573 | 913 |
|numericValue s scaleChar factor scale| |
1292 | 914 |
|
915 |
fileSizeString isEmpty ifTrue:[ |
|
916 |
^ 0 |
|
917 |
]. |
|
918 |
||
919 |
scale := 1. |
|
920 |
s := fileSizeString readStream. |
|
921 |
numericValue := Number readFrom:s onError:[0]. |
|
922 |
s skipSeparators. |
|
923 |
s atEnd ifFalse:[ |
|
924 |
scaleChar := s next asLowercase. |
|
3573 | 925 |
factor := 1000. |
926 |
s peek == $i ifTrue:[ |
|
927 |
factor := 1024 |
|
928 |
]. |
|
3486 | 929 |
scaleChar == $k ifTrue:[ |
3573 | 930 |
scale := factor |
1292 | 931 |
] ifFalse:[ |
3486 | 932 |
scaleChar == $m ifTrue:[ |
3573 | 933 |
scale := factor * factor |
1292 | 934 |
] ifFalse:[ |
3486 | 935 |
scaleChar == $g ifTrue:[ |
3573 | 936 |
scale := factor * factor * factor |
1292 | 937 |
] ifFalse:[ |
3486 | 938 |
scaleChar == $t ifTrue:[ |
3573 | 939 |
scale := factor * factor * factor * factor |
3142 | 940 |
] ifFalse:[ |
3486 | 941 |
scaleChar == $p ifTrue:[ |
3573 | 942 |
scale := factor * factor * factor * factor * factor |
3142 | 943 |
] ifFalse:[ |
3486 | 944 |
scaleChar == $e ifTrue:[ |
3573 | 945 |
scale := factor * factor * factor * factor * factor * factor |
946 |
] ifFalse:[ |
|
947 |
scaleChar == $z ifTrue:[ |
|
948 |
scale := factor * factor * factor * factor * factor * factor * factor |
|
949 |
] ifFalse:[ |
|
950 |
scaleChar == $y ifTrue:[ |
|
951 |
scale := factor * factor * factor * factor * factor * factor * factor * factor |
|
952 |
] |
|
953 |
] |
|
3142 | 954 |
] |
955 |
] |
|
1292 | 956 |
] |
957 |
] |
|
958 |
] |
|
959 |
]. |
|
960 |
]. |
|
961 |
^ (numericValue*scale) asInteger |
|
962 |
||
963 |
" |
|
964 |
self fileSizeFromString:'1234' |
|
1295 | 965 |
self fileSizeFromString:'10 k' |
3573 | 966 |
self fileSizeFromString:'10 kB' |
967 |
self fileSizeFromString:'10 KiB' |
|
1295 | 968 |
self fileSizeFromString:'0.6 k' |
969 |
self fileSizeFromString:'1.8 M' |
|
3573 | 970 |
self fileSizeFromString:'1.8 MB' |
971 |
self fileSizeFromString:'1.8 MiB' |
|
1295 | 972 |
self fileSizeFromString:'0.5 G' |
973 |
self fileSizeFromString:'0.5 T' |
|
3142 | 974 |
self fileSizeFromString:'0.5 P' |
975 |
self fileSizeFromString:'0.5 E' |
|
1292 | 976 |
" |
3142 | 977 |
|
978 |
"Modified (comment): / 17-12-2013 / 16:56:56 / cg" |
|
1292 | 979 |
! |
980 |
||
3575 | 981 |
fileSizeSIStringFor:nBytes |
982 |
"return a useful string for a size-in-bytes in SI standard notation; |
|
3573 | 983 |
Scales by K (*1000), M (*1000*1000) or G (*1000*1000*1000) or even T |
1294 | 984 |
as required and useful. |
3573 | 985 |
Provided here since this is so common... |
986 |
Notice: the scale is 1000 |
|
987 |
the 1000 scale has been standardized in 1998 by IEC to MB, GB, etc. |
|
988 |
See https://en.wikipedia.org/wiki/Mebibyte" |
|
1143 | 989 |
|
3573 | 990 |
^ self |
991 |
unitStringFor:nBytes |
|
992 |
scale:1000 |
|
3575 | 993 |
rounded:true |
3576 | 994 |
unitStrings:#('Byte' 'KB' 'MB' 'GB' 'TB' 'PB' 'EB' 'ZB' 'YB') |
3575 | 995 |
|
996 |
"/ |unitString n| |
|
997 |
"/ |
|
998 |
"/ nBytes isNil ifTrue:[ |
|
999 |
"/ ^ '-' |
|
1000 |
"/ ]. |
|
1001 |
"/ |
|
1002 |
"/ nBytes < (1000 * 1024 * 1024 * 1024) ifTrue:[ |
|
1003 |
"/ nBytes < (1000 * 1024 * 1024) ifTrue:[ |
|
1004 |
"/ nBytes < (1000 * 1024) ifTrue:[ |
|
1005 |
"/ nBytes < 1000 ifTrue:[ |
|
1006 |
"/ n := nBytes. |
|
1007 |
"/ unitString := ' '. |
|
1008 |
"/ ] ifFalse:[ |
|
1009 |
"/ n := (nBytes / 1024) asFixedPoint:1. |
|
1010 |
"/ unitString := ' K' |
|
1011 |
"/ ] |
|
1012 |
"/ ] ifFalse:[ |
|
1013 |
"/ n := (nBytes / (1024 * 1024)) asFixedPoint:1. |
|
1014 |
"/ unitString := ' M' |
|
1015 |
"/ ]. |
|
1016 |
"/ ] ifFalse:[ |
|
1017 |
"/ n := (nBytes / 1024 / (1024 * 1024)) asFixedPoint:1. |
|
1018 |
"/ unitString := ' G' |
|
1019 |
"/ ] |
|
1020 |
"/ ] ifFalse:[ |
|
1021 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
1022 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
1023 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
1024 |
"/ unitString := ' E' |
|
1025 |
"/ ] ifFalse:[ |
|
1026 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / 1024) asFixedPoint:1. |
|
1027 |
"/ unitString := ' P' |
|
1028 |
"/ ] |
|
1029 |
"/ ] ifFalse:[ |
|
1030 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
1031 |
"/ unitString := ' T' |
|
1032 |
"/ ]. |
|
1033 |
"/ ]. |
|
1034 |
"/ |
|
1035 |
"/"/ n truncated = n ifTrue:[ |
|
1036 |
"/"/ n := n truncated |
|
1037 |
"/"/ ]. |
|
1038 |
"/ ^ n printString , unitString. |
|
1039 |
||
1040 |
" |
|
1041 |
self fileSizeStringFor:nil |
|
1042 |
self fileSizeStringFor:10 |
|
1043 |
self fileSizeStringFor:100 |
|
1044 |
self fileSizeStringFor:1000 |
|
1045 |
self fileSizeStringFor:8192 |
|
1046 |
self fileSizeStringFor:10000 |
|
1047 |
self fileSizeStringFor:100000 |
|
1048 |
self fileSizeStringFor:1000000 |
|
1049 |
self fileSizeStringFor:10000000 |
|
1050 |
self fileSizeStringFor:100000000 |
|
1051 |
self fileSizeStringFor:1000000000 |
|
1052 |
self fileSizeStringFor:10000000000 |
|
1053 |
self fileSizeStringFor:100000000000 |
|
1054 |
self fileSizeStringFor:1000000000000 |
|
1055 |
self fileSizeStringFor:10000000000000 |
|
1056 |
self fileSizeStringFor:10000000000000000 |
|
1057 |
self fileSizeStringFor:100000000000000000000 |
|
1058 |
self fileSizeStringFor:1000000000000000000000 |
|
1059 |
" |
|
1060 |
||
1061 |
"Modified: / 17-12-2013 / 16:55:17 / cg" |
|
1062 |
"Modified (comment): / 18-12-2014 / 09:54:33 / sr" |
|
1063 |
! |
|
1064 |
||
1065 |
fileSizeStringFor:nBytes |
|
1066 |
"return a useful string for a size-in-bytes. |
|
1067 |
Scales either by 1000 or 1024, depending on the user preferences. |
|
1068 |
See https://en.wikipedia.org/wiki/Mebibyte" |
|
1069 |
||
1070 |
^ self |
|
1071 |
unitStringFor:nBytes |
|
1072 |
scale:(UserPreferences current unitForFileSize) |
|
1073 |
rounded:true |
|
1074 |
unitStrings:(UserPreferences current unitStringsForFileSize) |
|
3457 | 1075 |
|
3573 | 1076 |
"/ |unitString n| |
1077 |
"/ |
|
1078 |
"/ nBytes isNil ifTrue:[ |
|
1079 |
"/ ^ '-' |
|
1346 | 1080 |
"/ ]. |
3573 | 1081 |
"/ |
1082 |
"/ nBytes < (1000 * 1024 * 1024 * 1024) ifTrue:[ |
|
1083 |
"/ nBytes < (1000 * 1024 * 1024) ifTrue:[ |
|
1084 |
"/ nBytes < (1000 * 1024) ifTrue:[ |
|
1085 |
"/ nBytes < 1000 ifTrue:[ |
|
1086 |
"/ n := nBytes. |
|
1087 |
"/ unitString := ' '. |
|
1088 |
"/ ] ifFalse:[ |
|
1089 |
"/ n := (nBytes / 1024) asFixedPoint:1. |
|
1090 |
"/ unitString := ' K' |
|
1091 |
"/ ] |
|
1092 |
"/ ] ifFalse:[ |
|
1093 |
"/ n := (nBytes / (1024 * 1024)) asFixedPoint:1. |
|
1094 |
"/ unitString := ' M' |
|
1095 |
"/ ]. |
|
1096 |
"/ ] ifFalse:[ |
|
1097 |
"/ n := (nBytes / 1024 / (1024 * 1024)) asFixedPoint:1. |
|
1098 |
"/ unitString := ' G' |
|
1099 |
"/ ] |
|
1100 |
"/ ] ifFalse:[ |
|
1101 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
1102 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
1103 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
1104 |
"/ unitString := ' E' |
|
1105 |
"/ ] ifFalse:[ |
|
1106 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / 1024) asFixedPoint:1. |
|
1107 |
"/ unitString := ' P' |
|
1108 |
"/ ] |
|
1109 |
"/ ] ifFalse:[ |
|
1110 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
1111 |
"/ unitString := ' T' |
|
1112 |
"/ ]. |
|
1113 |
"/ ]. |
|
1114 |
"/ |
|
1115 |
"/"/ n truncated = n ifTrue:[ |
|
1116 |
"/"/ n := n truncated |
|
1117 |
"/"/ ]. |
|
1118 |
"/ ^ n printString , unitString. |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1119 |
|
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1120 |
" |
3457 | 1121 |
self fileSizeStringFor:nil |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1122 |
self fileSizeStringFor:10 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1123 |
self fileSizeStringFor:100 |
1298
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1124 |
self fileSizeStringFor:1000 |
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1125 |
self fileSizeStringFor:8192 |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1126 |
self fileSizeStringFor:10000 |
1298
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1127 |
self fileSizeStringFor:100000 |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1128 |
self fileSizeStringFor:1000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1129 |
self fileSizeStringFor:10000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1130 |
self fileSizeStringFor:100000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1131 |
self fileSizeStringFor:1000000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1132 |
self fileSizeStringFor:10000000000 |
1293 | 1133 |
self fileSizeStringFor:100000000000 |
1134 |
self fileSizeStringFor:1000000000000 |
|
1135 |
self fileSizeStringFor:10000000000000 |
|
3141 | 1136 |
self fileSizeStringFor:10000000000000000 |
3142 | 1137 |
self fileSizeStringFor:100000000000000000000 |
1138 |
self fileSizeStringFor:1000000000000000000000 |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1139 |
" |
3141 | 1140 |
|
3142 | 1141 |
"Modified: / 17-12-2013 / 16:55:17 / cg" |
3457 | 1142 |
"Modified (comment): / 18-12-2014 / 09:54:33 / sr" |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1143 |
! |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1144 |
|
3120 | 1145 |
frequencyFromString:frequencyString |
1146 |
"given a frequency string (i.e. a number followed by optional kHz, Mhz or Hhz as required. |
|
1147 |
Provided here since this is so common..." |
|
1148 |
||
1149 |
|numericValue s scaleChar scale rest| |
|
1150 |
||
1151 |
frequencyString isEmpty ifTrue:[ |
|
1152 |
^ 0 |
|
1153 |
]. |
|
1154 |
||
1155 |
scale := 1. |
|
1156 |
s := frequencyString readStream. |
|
1157 |
numericValue := Number readFrom:s onError:[0]. |
|
1158 |
s skipSeparators. |
|
1159 |
s atEnd ifFalse:[ |
|
1160 |
scaleChar := s next asLowercase. |
|
3486 | 1161 |
scaleChar == $/ ifTrue:[ |
3120 | 1162 |
rest := s upToSeparator asLowercase. |
1163 |
(rest = 'm' or:[rest = 'min']) ifTrue:[ |
|
1164 |
scale := 1/60 |
|
1165 |
] ifFalse:[ |
|
1166 |
(rest = 'h' or:[rest = 'hr' or:[rest = 'hour']]) ifTrue:[ |
|
1167 |
scale := 1/60/60 |
|
1168 |
] ifFalse:[ |
|
1169 |
(rest = 'd' or:[rest = 'dy' or:[rest = 'day']]) ifTrue:[ |
|
1170 |
scale := 1/60/60/24 |
|
1171 |
]. |
|
1172 |
]. |
|
1173 |
]. |
|
1174 |
] ifFalse:[ |
|
3486 | 1175 |
scaleChar == $k ifTrue:[ |
3120 | 1176 |
scale := 1000 |
1177 |
] ifFalse:[ |
|
3486 | 1178 |
scaleChar == $m ifTrue:[ |
3120 | 1179 |
scale := 1000 * 1000 |
1180 |
] ifFalse:[ |
|
3487 | 1181 |
scaleChar == $g ifTrue:[ |
3120 | 1182 |
scale := 1000 * 1000 * 1000 |
1183 |
] ifFalse:[ |
|
3487 | 1184 |
scaleChar == $t ifTrue:[ |
3120 | 1185 |
scale := 1000 * 1000 * 1000 * 1000 |
1186 |
] |
|
1187 |
] |
|
1188 |
] |
|
1189 |
]. |
|
1190 |
] |
|
1191 |
]. |
|
1192 |
^ (numericValue*scale) |
|
1193 |
||
1194 |
" |
|
1195 |
self frequencyFromString:'1234' |
|
1196 |
self frequencyFromString:'10 kHz' |
|
1197 |
self frequencyFromString:'0.6 k' |
|
1198 |
self frequencyFromString:'1.8 Mhz' |
|
1199 |
self frequencyFromString:'0.5 Ghz' |
|
1200 |
self frequencyFromString:'0.5 Thz' |
|
3577 | 1201 |
self frequencyFromString:'1000/s' |
3120 | 1202 |
self frequencyFromString:'10/s' |
1203 |
self frequencyFromString:'10/min' |
|
1204 |
self frequencyFromString:'10/hr' |
|
1205 |
" |
|
1206 |
! |
|
1207 |
||
1208 |
frequencyStringFor:hertz |
|
1209 |
"return a useful string for a frequency; |
|
1210 |
Scales by K (*1000), M (*1000*1000) or G (*1000*1000*1000) or even T |
|
1211 |
as required and useful. |
|
1212 |
Provided here since this is so common..." |
|
1213 |
||
3573 | 1214 |
^ self |
1215 |
unitStringFor:hertz |
|
1216 |
scale:1000 |
|
3574 | 1217 |
rounded:false |
3576 | 1218 |
unitStrings:#('Hz' 'Khz' 'Mhz' 'Ghz' 'Thz' 'Phz' 'Ehz') |
3120 | 1219 |
|
1220 |
" |
|
3577 | 1221 |
self frequencyStringFor:0.1 |
3120 | 1222 |
self frequencyStringFor:10 |
1223 |
self frequencyStringFor:100 |
|
1224 |
self frequencyStringFor:1000 |
|
1225 |
self frequencyStringFor:8192 |
|
1226 |
self frequencyStringFor:10000 |
|
1227 |
self frequencyStringFor:100000 |
|
1228 |
self frequencyStringFor:1000000 |
|
1229 |
self frequencyStringFor:10000000 |
|
1230 |
self frequencyStringFor:100000000 |
|
1231 |
self frequencyStringFor:1000000000 |
|
1232 |
self frequencyStringFor:10000000000 |
|
1233 |
self frequencyStringFor:100000000000 |
|
1234 |
self frequencyStringFor:1000000000000 |
|
1235 |
self frequencyStringFor:10000000000000 |
|
1236 |
" |
|
1237 |
! |
|
1238 |
||
363 | 1239 |
inchToMillimeter:inches |
364 | 1240 |
"convert inches to mm. Made this an extra method, since its so common." |
363 | 1241 |
|
1242 |
^ inches * 25.4 |
|
1243 |
||
1244 |
"Created: 31.5.1996 / 13:37:31 / cg" |
|
364 | 1245 |
"Modified: 31.5.1996 / 18:08:14 / cg" |
363 | 1246 |
! |
1247 |
||
1248 |
millimeterToInch:mm |
|
364 | 1249 |
"convert mm to inches. Made this an extra method, since its so common." |
363 | 1250 |
|
1251 |
^ mm / 25.4 |
|
1252 |
||
1253 |
"Created: 31.5.1996 / 13:37:48 / cg" |
|
364 | 1254 |
"Modified: 31.5.1996 / 18:08:20 / cg" |
3573 | 1255 |
! |
1256 |
||
3574 | 1257 |
unitStringFor:nBytes scale:k rounded:rounded unitStrings:unitStrings |
3573 | 1258 |
"common helper for SI (1000-based) and binary (1024-based) human readable string generators. |
3574 | 1259 |
Notice that both forms are often encountered and useful. |
1260 |
If rounded is true, round to nearest integer" |
|
3573 | 1261 |
|
3576 | 1262 |
|n kN idx val unitString| |
3573 | 1263 |
|
1264 |
nBytes isNil ifTrue:[ |
|
1265 |
^ '-' |
|
1266 |
]. |
|
1267 |
||
1268 |
idx := 1. |
|
1269 |
n := nBytes. |
|
1270 |
kN := 1. |
|
1271 |
[ (idx < unitStrings size) and:[ nBytes >= (kN * k) ] ] whileTrue:[ |
|
1272 |
idx := idx + 1. |
|
1273 |
kN := kN * k. |
|
1274 |
]. |
|
3575 | 1275 |
|
1276 |
val := nBytes / kN. |
|
3576 | 1277 |
unitString := ' ',(unitStrings at:idx). |
1278 |
||
3575 | 1279 |
((rounded and:[val > 20]) or:[val isInteger]) ifTrue:[ |
3576 | 1280 |
^ (val rounded) printString , unitString |
3574 | 1281 |
]. |
3576 | 1282 |
^ (val asFixedPoint:1) printString , unitString |
3573 | 1283 |
|
1284 |
" |
|
1285 |
self fileSizeStringFor:nil |
|
1286 |
self fileSizeStringFor:10 |
|
1287 |
self fileSizeStringFor:100 |
|
3574 | 1288 |
self fileSizeStringFor:900 |
1289 |
self fileSizeStringFor:1100 |
|
1290 |
self fileSizeStringFor:1400 |
|
1291 |
self fileSizeStringFor:1499 |
|
1292 |
self fileSizeStringFor:1500 |
|
1293 |
||
3573 | 1294 |
self fileSizeStringFor:1000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
1295 |
self fileSizeStringFor:8192 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1296 |
self fileSizeStringFor:10000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1297 |
self fileSizeStringFor:100000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1298 |
self fileSizeStringFor:1000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1299 |
self fileSizeStringFor:10000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1300 |
self fileSizeStringFor:100000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1301 |
self fileSizeStringFor:1000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1302 |
self fileSizeStringFor:10000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1303 |
self fileSizeStringFor:100000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1304 |
self fileSizeStringFor:1000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1305 |
self fileSizeStringFor:10000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1306 |
self fileSizeStringFor:10000000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1307 |
self fileSizeStringFor:100000000000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1308 |
self fileSizeStringFor:1000000000000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1309 |
||
1310 |
self fileSizeStringFor:8192 scale:1024 unitStrings:#('' 'KiB' 'MiB' 'GiB' 'TiB' 'PiB' 'EiB' ) |
|
1311 |
self fileSizeStringFor:81920000 scale:1024 unitStrings:#('' 'KiB' 'MiB' 'GiB' 'TiB' 'PiB' 'EiB' ) |
|
1312 |
" |
|
1313 |
||
1314 |
"Modified: / 17-12-2013 / 16:55:17 / cg" |
|
1315 |
"Modified (comment): / 18-12-2014 / 09:54:33 / sr" |
|
363 | 1316 |
! ! |
1317 |
||
516 | 1318 |
!UnitConverter class methodsFor:'missing conversion'! |
1319 |
||
1320 |
noConversionFrom:sourceUnit to:destUnit |
|
1321 |
Transcript showCR:'no conversion ' , sourceUnit printString , ' -> ' , destUnit printString. |
|
1322 |
^ nil |
|
1323 |
||
1324 |
"Created: 29.3.1997 / 17:56:03 / cg" |
|
1325 |
! ! |
|
1326 |
||
363 | 1327 |
!UnitConverter class methodsFor:'private'! |
1328 |
||
1329 |
addConversion:factor from:sourceMetric to:destMetric |
|
1330 |
"add a conversion" |
|
1331 |
||
1332 |
|conversion| |
|
1333 |
||
1334 |
conversion := Conversions at:sourceMetric ifAbsent:nil. |
|
1335 |
conversion isNil ifTrue:[ |
|
601 | 1336 |
conversion := IdentityDictionary new. |
1337 |
Conversions at:sourceMetric put:conversion |
|
363 | 1338 |
]. |
1339 |
conversion at:destMetric put:factor. |
|
1340 |
^ conversion |
|
1341 |
||
1342 |
"Created: 31.5.1996 / 13:51:25 / cg" |
|
515 | 1343 |
"Modified: 29.3.1997 / 17:19:31 / cg" |
363 | 1344 |
! |
1345 |
||
1346 |
convertDirect:howMany from:sourceMetric to:destMetric |
|
1347 |
"given a value in sourceMetric (symbolic), try to convert it |
|
1348 |
to destMetric (symbolic); |
|
1349 |
Only direct conversions are tried; return nil, if the conversion is unknown." |
|
1350 |
||
1351 |
|conversion factor| |
|
1352 |
||
550 | 1353 |
Conversions isNil ifTrue:[ |
601 | 1354 |
self initializeConversions |
550 | 1355 |
]. |
1356 |
||
363 | 1357 |
conversion := Conversions at:sourceMetric ifAbsent:nil. |
1358 |
conversion isNil ifTrue:[^ nil]. |
|
1359 |
||
1360 |
factor := conversion at:destMetric ifAbsent:nil. |
|
1361 |
factor isNil ifTrue:[^ nil]. |
|
1362 |
||
364 | 1363 |
factor isNumber ifTrue:[ |
601 | 1364 |
^ howMany * factor |
364 | 1365 |
]. |
515 | 1366 |
^ factor value:howMany |
363 | 1367 |
|
1368 |
" |
|
1369 |
UnitConverter convertDirect:1 from:#inch to:#meter |
|
1370 |
UnitConverter convertDirect:1 from:#inch to:#millimeter |
|
515 | 1371 |
UnitConverter convertDirect:1 from:#degrees to:#fahrenheit |
363 | 1372 |
" |
1373 |
||
1374 |
"Created: 31.5.1996 / 13:54:33 / cg" |
|
550 | 1375 |
"Modified: 22.7.1997 / 13:57:52 / cg" |
363 | 1376 |
! |
1377 |
||
1378 |
unscaled:what |
|
1379 |
"given a unit, return the base and a factor as assoc, |
|
1380 |
or nil if not found" |
|
1381 |
||
550 | 1382 |
Conversions isNil ifTrue:[ |
2995 | 1383 |
self initializeConversions |
550 | 1384 |
]. |
1385 |
||
363 | 1386 |
Scaling keysAndValuesDo:[:name :factor | |
2995 | 1387 |
|rest| |
363 | 1388 |
|
2995 | 1389 |
(what startsWith:name) ifTrue:[ |
1390 |
rest := what withoutPrefix:name. |
|
1391 |
rest := rest asSymbolIfInterned. |
|
1392 |
rest notNil ifTrue:[ |
|
1393 |
^ rest -> factor. |
|
1394 |
] |
|
1395 |
]. |
|
363 | 1396 |
]. |
1397 |
^ nil |
|
1398 |
||
1399 |
" |
|
1400 |
UnitConverter unscaled:#millimeter |
|
1401 |
UnitConverter unscaled:#nanometer |
|
1402 |
UnitConverter unscaled:#kilometer |
|
1403 |
UnitConverter unscaled:#fuzzymeter |
|
1404 |
" |
|
1405 |
||
550 | 1406 |
"Modified: 22.7.1997 / 13:57:46 / cg" |
363 | 1407 |
! ! |
1408 |
||
3650 | 1409 |
!UnitConverter class methodsFor:'queries'! |
1410 |
||
1411 |
isUtilityClass |
|
1412 |
^ self == UnitConverter |
|
1413 |
! ! |
|
1414 |
||
363 | 1415 |
!UnitConverter class methodsFor:'documentation'! |
1416 |
||
1417 |
version |
|
3573 | 1418 |
^ '$Header$' |
2320 | 1419 |
! |
1420 |
||
1421 |
version_CVS |
|
3573 | 1422 |
^ '$Header$' |
363 | 1423 |
! ! |
2988
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
1424 |