author | Claus Gittinger <cg@exept.de> |
Mon, 22 Jun 2015 16:35:24 +0200 | |
changeset 3575 | f860f717ad6f |
parent 3574 | b56b7752fa19 |
child 3576 | 9a273867e732 |
permissions | -rw-r--r-- |
3573 | 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] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
148 |
thats 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 |
||
318 |
"/ german area - add your countries, and return to me ... |
|
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, |
|
327 |
"/ thats also 1/4th of a good wine ;-) |
|
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 |
|
392 |
" |
|
393 |
Conversions := nil. |
|
394 |
UnitConverter initializeConversions |
|
395 |
" |
|
396 |
||
397 |
"Created: / 22.7.1997 / 13:56:40 / cg" |
|
398 |
"Modified: / 10.10.2001 / 14:39:04 / cg" |
|
399 |
! |
|
400 |
||
401 |
initializePrintValues |
|
363 | 402 |
"/ inch to (roughly) a typesetter point |
403 |
self addConversion:(1/72) from:#point to:#inch. |
|
404 |
||
405 |
"/ point to twips; 20 twips (as in Rich-Text-Format) make a point |
|
406 |
self addConversion:(1/20) from:#twip to:#point. |
|
407 |
||
365 | 408 |
"/ US paper |
409 |
self addConversion:11 from:#'letter-page-height' to:#inch. |
|
410 |
self addConversion:8.5 from:#'letter-page-width' to:#inch. |
|
411 |
self addConversion:8.5 from:#'letter-landscape-page-height' to:#inch. |
|
412 |
self addConversion:11 from:#'letter-landscape-page-width' to:#inch. |
|
413 |
||
385 | 414 |
self addConversion:14 from:#'legal-page-height' to:#inch. |
415 |
self addConversion:8.5 from:#'legal-page-width' to:#inch. |
|
416 |
self addConversion:8.5 from:#'legal-landscape-page-height' to:#inch. |
|
417 |
self addConversion:14 from:#'legal-landscape-page-width' to:#inch. |
|
418 |
||
419 |
self addConversion:11 from:#'ledger-page-height' to:#inch. |
|
420 |
self addConversion:17 from:#'ledger-page-width' to:#inch. |
|
421 |
self addConversion:17 from:#'ledger-landscape-page-height' to:#inch. |
|
422 |
self addConversion:11 from:#'ledger-landscape-page-width' to:#inch. |
|
423 |
||
365 | 424 |
"/ European paper |
425 |
self addConversion:840 from:#'a1-page-height' to:#millimeter. |
|
426 |
self addConversion:592 from:#'a1-page-width' to:#millimeter. |
|
427 |
self addConversion:592 from:#'a1-landscape-page-height' to:#millimeter. |
|
428 |
self addConversion:840 from:#'a1-landscape-page-width' to:#millimeter. |
|
429 |
||
430 |
self addConversion:592 from:#'a2-page-height' to:#millimeter. |
|
431 |
self addConversion:420 from:#'a2-page-width' to:#millimeter. |
|
432 |
self addConversion:420 from:#'a2-landscape-page-height' to:#millimeter. |
|
433 |
self addConversion:592 from:#'a2-landscape-page-width' to:#millimeter. |
|
434 |
||
435 |
self addConversion:420 from:#'a3-page-height' to:#millimeter. |
|
436 |
self addConversion:296 from:#'a3-page-width' to:#millimeter. |
|
437 |
self addConversion:296 from:#'a3-landscape-page-height' to:#millimeter. |
|
438 |
self addConversion:420 from:#'a3-landscape-page-width' to:#millimeter. |
|
439 |
||
440 |
self addConversion:296 from:#'a4-page-height' to:#millimeter. |
|
441 |
self addConversion:210 from:#'a4-page-width' to:#millimeter. |
|
442 |
self addConversion:210 from:#'a4-landscape-page-height' to:#millimeter. |
|
443 |
self addConversion:296 from:#'a4-landscape-page-width' to:#millimeter. |
|
444 |
||
445 |
self addConversion:210 from:#'a5-page-height' to:#millimeter. |
|
446 |
self addConversion:148 from:#'a5-page-width' to:#millimeter. |
|
447 |
self addConversion:148 from:#'a5-landscape-page-height' to:#millimeter. |
|
448 |
self addConversion:210 from:#'a5-landscape-page-width' to:#millimeter. |
|
449 |
||
450 |
self addConversion:148 from:#'a6-page-height' to:#millimeter. |
|
451 |
self addConversion:105 from:#'a6-page-width' to:#millimeter. |
|
452 |
self addConversion:105 from:#'a6-landscape-page-height' to:#millimeter. |
|
453 |
self addConversion:148 from:#'a6-landscape-page-width' to:#millimeter. |
|
454 |
||
385 | 455 |
self addConversion:257 from:#'b5-page-height' to:#millimeter. |
456 |
self addConversion:182 from:#'b5-page-width' to:#millimeter. |
|
457 |
self addConversion:182 from:#'b5-landscape-page-height' to:#millimeter. |
|
458 |
self addConversion:257 from:#'b5-landscape-page-width' to:#millimeter. |
|
459 |
||
460 |
||
365 | 461 |
Aliases at:#letterW put:#'letter-page-width'. |
462 |
Aliases at:#letterH put:#'letter-page-height'. |
|
385 | 463 |
Aliases at:#legalW put:#'legal-page-width'. |
464 |
Aliases at:#legalH put:#'legal-page-height'. |
|
465 |
Aliases at:#ledgerW put:#'ledger-page-width'. |
|
466 |
Aliases at:#ledgerH put:#'ledger-page-height'. |
|
365 | 467 |
Aliases at:#a1W put:#'a1-page-width'. |
468 |
Aliases at:#a1H put:#'a1-page-height'. |
|
469 |
Aliases at:#a2W put:#'a2-page-width'. |
|
470 |
Aliases at:#a2H put:#'a2-page-height'. |
|
471 |
Aliases at:#a3W put:#'a3-page-width'. |
|
472 |
Aliases at:#a3H put:#'a3-page-height'. |
|
473 |
Aliases at:#a4W put:#'a4-page-width'. |
|
474 |
Aliases at:#a4H put:#'a4-page-height'. |
|
475 |
Aliases at:#a5W put:#'a5-page-width'. |
|
476 |
Aliases at:#a5H put:#'a5-page-height'. |
|
477 |
Aliases at:#a6W put:#'a6-page-width'. |
|
478 |
Aliases at:#a6H put:#'a6-page-height'. |
|
385 | 479 |
Aliases at:#b5W put:#'b5-page-width'. |
480 |
Aliases at:#b5H put:#'b5-page-height'. |
|
365 | 481 |
|
482 |
Aliases at:#letterlW put:#'letter-landscape-page-width'. |
|
483 |
Aliases at:#letterlH put:#'letter-landscape-page-height'. |
|
385 | 484 |
Aliases at:#legallW put:#'legal-landscape-page-width'. |
485 |
Aliases at:#legallH put:#'legal-landscape-page-height'. |
|
486 |
Aliases at:#ledgerlW put:#'ledger-landscape-page-width'. |
|
487 |
Aliases at:#ledgerlH put:#'ledger-landscape-page-height'. |
|
365 | 488 |
Aliases at:#a1lW put:#'a1-landscape-page-width'. |
489 |
Aliases at:#a1lH put:#'a1-landscape-page-height'. |
|
490 |
Aliases at:#a2lW put:#'a2-landscape-page-width'. |
|
491 |
Aliases at:#a2lH put:#'a2-landscape-page-height'. |
|
492 |
Aliases at:#a3lW put:#'a3-landscape-page-width'. |
|
493 |
Aliases at:#a3lH put:#'a3-landscape-page-height'. |
|
494 |
Aliases at:#a4lW put:#'a4-landscape-page-width'. |
|
495 |
Aliases at:#a4lH put:#'a4-landscape-page-height'. |
|
496 |
Aliases at:#a5lW put:#'a5-landscape-page-width'. |
|
497 |
Aliases at:#a5lH put:#'a5-landscape-page-height'. |
|
498 |
Aliases at:#a6lW put:#'a6-landscape-page-width'. |
|
499 |
Aliases at:#a6lH put:#'a6-landscape-page-height'. |
|
385 | 500 |
Aliases at:#b5lW put:#'b5-landscape-page-width'. |
501 |
Aliases at:#b5lH put:#'b5-landscape-page-height'. |
|
1135 | 502 |
! |
363 | 503 |
|
1135 | 504 |
initializeScaleFactors |
505 |
Scaling := IdentityDictionary new. |
|
363 | 506 |
|
1285 | 507 |
Scaling at:#yotta put:1000000000000000000000000. "/ 10^24 |
508 |
Scaling at:#zetta put:1000000000000000000000. "/ 10^21 |
|
509 |
Scaling at:#exa put:1000000000000000000. "/ 10^18 |
|
510 |
Scaling at:#peta put:1000000000000000. "/ 10^15 |
|
511 |
Scaling at:#tera put:1000000000000. "/ 10^12 |
|
512 |
Scaling at:#giga put:1000000000. "/ 10^9 |
|
513 |
Scaling at:#mega put:1000000. "/ 10^6 |
|
1135 | 514 |
Scaling at:#myria put:10000. |
1285 | 515 |
Scaling at:#kilo put:1000. "/ 10^3 |
516 |
Scaling at:#hecto put:100. "/ 10^2 |
|
517 |
Scaling at:#deca put:10. "/ 10^1 |
|
2320 | 518 |
Scaling at:#deka put:10. "/ 10^1 |
1323 | 519 |
Scaling at:#deci put:(1/10). "/ 10^-1 |
1135 | 520 |
Scaling at:#centi put:(1/100). |
1323 | 521 |
Scaling at:#milli put:(1/1000). "/ 10^-3 |
522 |
Scaling at:#micro put:(1/1000000). "/ 10^-6 |
|
523 |
Scaling at:#nano put:(1/1000000000). "/ 10^-9 |
|
524 |
Scaling at:#pico put:(1/1000000000000). "/ 10^-12 |
|
525 |
Scaling at:#femto put:(1/1000000000000000). "/ 10^-15 |
|
526 |
Scaling at:#atto put:(1/1000000000000000000). "/ 10^-18 |
|
527 |
Scaling at:#zepto put:(1/1000000000000000000000). "/ 10^-21 |
|
528 |
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
|
529 |
! ! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
530 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
531 |
!UnitConverter class methodsFor:'accessing'! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
532 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
533 |
scalings |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
534 |
"return the set of known scaling prefixes" |
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 |
^ Scaling keys |
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 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
539 |
UnitConverter scalings |
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 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
542 |
"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
|
543 |
"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
|
544 |
! |
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 |
units |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
547 |
"return the set of known units" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
548 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
549 |
|setOfUnits| |
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 |
setOfUnits := IdentitySet new. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
552 |
Conversions keysAndValuesDo:[:srcUnit :conversionInfo | |
601 | 553 |
setOfUnits add:srcUnit. |
554 |
conversionInfo keysDo:[:targetUnit | |
|
555 |
setOfUnits add:targetUnit |
|
556 |
] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
557 |
]. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
558 |
^ setOfUnits |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
559 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
560 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
561 |
UnitConverter units |
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 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
564 |
"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
|
565 |
"Modified: 6.8.1997 / 16:57:47 / cg" |
363 | 566 |
! ! |
567 |
||
568 |
!UnitConverter class methodsFor:'conversions'! |
|
569 |
||
570 |
convert:howMany from:sourceUnit to:destUnit |
|
571 |
"given a value in sourceUnit (symbolic), try to convert it |
|
572 |
to destUnit (symbolic); return nil, if the conversion is |
|
573 |
unknown." |
|
574 |
||
516 | 575 |
|u conversions alias rslt sU dU const val unit |
576 |
i suNumerator suDenominator duNumerator duDenominator uN uD |
|
577 |
sF1 sF2 dF1 dF2 |
|
578 |
s d| |
|
363 | 579 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
580 |
"/ Transcript showCR:('try ' , sourceUnit , '->' , destUnit). |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
581 |
|
550 | 582 |
Conversions isNil ifTrue:[ |
1031 | 583 |
self initializeConversions |
550 | 584 |
]. |
585 |
||
363 | 586 |
"/ somehow, recursion must end ... |
587 |
sourceUnit == destUnit ifTrue:[ |
|
1031 | 588 |
^ howMany |
363 | 589 |
]. |
590 |
||
369 | 591 |
sourceUnit isSymbol ifFalse:[ |
1031 | 592 |
s := sourceUnit withoutSeparators. |
593 |
sU := s asSymbolIfInterned. |
|
594 |
sU notNil ifTrue:[ |
|
595 |
^ self convert:howMany from:sU to:destUnit |
|
596 |
]. |
|
597 |
(s startsWith:$() ifTrue:[ |
|
598 |
(s endsWith:$)) ifTrue:[ |
|
599 |
s := s copyFrom:2 to:(s size - 1). |
|
600 |
^ self convert:howMany from:s to:destUnit |
|
601 |
] |
|
602 |
]. |
|
369 | 603 |
]. |
604 |
destUnit isSymbol ifFalse:[ |
|
1031 | 605 |
d := destUnit withoutSeparators. |
606 |
dU := d asSymbolIfInterned. |
|
607 |
dU notNil ifTrue:[ |
|
608 |
^ self convert:howMany from:sourceUnit to:dU |
|
609 |
]. |
|
610 |
(d startsWith:$() ifTrue:[ |
|
611 |
(d endsWith:$)) ifTrue:[ |
|
612 |
d := d copyFrom:2 to:(d size - 1). |
|
613 |
^ self convert:howMany from:sourceUnit to:d |
|
614 |
] |
|
615 |
]. |
|
369 | 616 |
]. |
617 |
||
363 | 618 |
"/ first, get rid of scalers ... |
619 |
||
620 |
u := self unscaled:sourceUnit. |
|
621 |
u notNil ifTrue:[ |
|
1031 | 622 |
^ self convert:(howMany*(u value)) from:(u key) to:destUnit |
363 | 623 |
]. |
624 |
||
625 |
u := self unscaled:destUnit. |
|
626 |
u notNil ifTrue:[ |
|
1031 | 627 |
^ self convert:(howMany/(u value)) from:sourceUnit to:(u key) |
363 | 628 |
]. |
629 |
||
630 |
"/ and of aliases ... |
|
631 |
||
632 |
alias := Aliases at:sourceUnit ifAbsent:nil. |
|
633 |
alias notNil ifTrue:[ |
|
1031 | 634 |
^ self convert:howMany from:alias to:destUnit |
363 | 635 |
]. |
636 |
alias := Aliases at:destUnit ifAbsent:nil. |
|
637 |
alias notNil ifTrue:[ |
|
1031 | 638 |
^ self convert:howMany from:sourceUnit to:alias |
363 | 639 |
]. |
640 |
||
516 | 641 |
"/ any constants ? |
642 |
||
643 |
(Constants includesKey:sourceUnit) ifTrue:[ |
|
1031 | 644 |
const := Constants at:sourceUnit. |
645 |
val := const at:1. |
|
646 |
unit := const at:2. |
|
647 |
^ self convert:(howMany*val) from:unit to:destUnit |
|
516 | 648 |
]. |
649 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
650 |
"/ compounds (^ , / or *) are very naively parsed |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
651 |
"/ 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
|
652 |
"/ 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
|
653 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
654 |
|
363 | 655 |
"/ working with squares or cubics ? |
656 |
||
516 | 657 |
((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
|
658 |
sU := (sourceUnit copyButLast:2) asSymbolIfInterned. |
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
659 |
dU := (destUnit copyButLast:2) asSymbolIfInterned. |
1031 | 660 |
(sU notNil and:[dU notNil]) ifTrue:[ |
661 |
^ (self convert:(howMany sqrt) from:sU to:dU) squared |
|
662 |
]. |
|
663 |
^ self noConversionFrom:sourceUnit to:destUnit. |
|
363 | 664 |
]. |
516 | 665 |
((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
|
666 |
sU := (sourceUnit copyButLast:2) asSymbolIfInterned. |
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
667 |
dU := (destUnit copyButLast:2) asSymbolIfInterned. |
1031 | 668 |
(sU notNil and:[dU notNil]) ifTrue:[ |
669 |
^ (self convert:(howMany raisedTo:(1/3)) from:sU to:dU) raisedTo:3 |
|
670 |
]. |
|
671 |
^ self noConversionFrom:sourceUnit to:destUnit. |
|
516 | 672 |
]. |
673 |
||
674 |
"/ working with fractions ? |
|
675 |
||
676 |
((sourceUnit includes:$/) and:[destUnit includes:$/]) ifTrue:[ |
|
1031 | 677 |
"/ look for a constant conversion factor |
678 |
i := sourceUnit indexOf:$/. |
|
679 |
suNumerator := sourceUnit copyTo:(i - 1). |
|
680 |
suDenominator := sourceUnit copyFrom:(i + 1). |
|
681 |
i := destUnit indexOf:$/. |
|
682 |
duNumerator := destUnit copyTo:(i - 1). |
|
683 |
duDenominator := destUnit copyFrom:(i + 1). |
|
516 | 684 |
|
1031 | 685 |
uN := self convert:howMany from:suNumerator to:duNumerator. |
686 |
uN notNil ifTrue:[ |
|
687 |
uD := self convert:1 from:suDenominator to:duDenominator. |
|
688 |
uD notNil ifTrue:[ |
|
689 |
^ uN / uD |
|
690 |
] |
|
691 |
]. |
|
516 | 692 |
]. |
693 |
||
694 |
"/ working with products ? |
|
695 |
||
696 |
((sourceUnit includes:$*) and:[destUnit includes:$*]) ifTrue:[ |
|
1031 | 697 |
i := sourceUnit indexOf:$*. |
698 |
sF1 := sourceUnit copyTo:(i - 1). |
|
699 |
sF2 := sourceUnit copyFrom:(i + 1). |
|
700 |
i := destUnit indexOf:$*. |
|
701 |
dF1 := destUnit copyTo:(i - 1). |
|
702 |
dF2 := destUnit copyFrom:(i + 1). |
|
516 | 703 |
|
1031 | 704 |
u := self convert:howMany from:sF1 to:dF1. |
705 |
u notNil ifTrue:[ |
|
706 |
u := self convert:u from:sF2 to:dF2. |
|
707 |
u notNil ifTrue:[ |
|
708 |
^ u |
|
709 |
] |
|
710 |
]. |
|
363 | 711 |
]. |
712 |
||
713 |
"/ the real work comes here ... |
|
714 |
||
715 |
"/ is there a direct conversion in the dataBase ? |
|
716 |
||
717 |
rslt := self convertDirect:howMany from:sourceUnit to:destUnit. |
|
718 |
rslt notNil ifTrue:[^ rslt]. |
|
719 |
||
720 |
"/ try inverse conversion ... |
|
721 |
||
722 |
rslt := self convertDirect:1 from:destUnit to:sourceUnit. |
|
723 |
rslt notNil ifTrue:[ ^ howMany / rslt]. |
|
724 |
||
516 | 725 |
"/ here's the deep recursion ... |
363 | 726 |
|
727 |
||
728 |
"/ try indirect conversion from source |
|
729 |
||
730 |
conversions := Conversions at:sourceUnit ifAbsent:nil. |
|
731 |
conversions notNil ifTrue:[ |
|
1031 | 732 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 | |
733 |
|factor2| |
|
363 | 734 |
|
1031 | 735 |
factor2 := self convert:factor1 from:intermediateUnit to:destUnit. |
736 |
factor2 notNil ifTrue:[^ factor2 * howMany]. |
|
737 |
]. |
|
363 | 738 |
]. |
739 |
||
740 |
"/ try indirect conversion from dest |
|
741 |
||
742 |
conversions := Conversions at:destUnit ifAbsent:nil. |
|
743 |
conversions notNil ifTrue:[ |
|
1031 | 744 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 | |
745 |
|factor2| |
|
363 | 746 |
|
1031 | 747 |
factor2 := self convert:(factor1) from:intermediateUnit to:sourceUnit. |
748 |
factor2 notNil ifTrue:[^ howMany / factor2]. |
|
749 |
]. |
|
363 | 750 |
]. |
751 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
752 |
"/ 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
|
753 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
754 |
(sourceUnit includes:$*) ifTrue:[ |
1031 | 755 |
i := sourceUnit indexOf:$*. |
756 |
sF1 := sourceUnit copyTo:(i - 1). |
|
757 |
sF2 := sourceUnit copyFrom:(i + 1). |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
758 |
|
1031 | 759 |
"/ see what we have ... |
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
760 |
|
1031 | 761 |
self units do:[:aUnit | |
762 |
|pref iUnit factor2 rslt| |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
763 |
|
1031 | 764 |
pref := sF1 , '*'. |
765 |
(aUnit startsWith:pref) ifTrue:[ |
|
766 |
"/ ok; want a/b -> x |
|
767 |
"/ found a/c -> any |
|
768 |
"/ what about c->b ? |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
769 |
|
1031 | 770 |
iUnit := aUnit copyFrom:pref size + 1. |
771 |
factor2 := self convert:1 from:sF2 to:iUnit. |
|
772 |
factor2 notNil ifTrue:[ |
|
773 |
"/ good ... |
|
774 |
rslt := self convert:(factor2 * howMany) |
|
775 |
from:aUnit to:destUnit. |
|
776 |
rslt notNil ifTrue:[^ rslt]. |
|
777 |
] |
|
778 |
] |
|
779 |
]. |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
780 |
]. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
781 |
|
516 | 782 |
^ self noConversionFrom:sourceUnit to:destUnit. |
363 | 783 |
|
784 |
"direct - how many meters are there in two inches: |
|
785 |
UnitConverter convert:2 from:#inch to:#meter |
|
515 | 786 |
UnitConverter convert:37 from:#degrees to:#fahrenheit |
363 | 787 |
|
788 |
reverse - how many inches are there in one meter |
|
789 |
UnitConverter convert:1 from:#meter to:#inch |
|
790 |
||
791 |
with alias: |
|
792 |
UnitConverter convert:1 from:#inch to:#m |
|
793 |
UnitConverter convert:1 from:#inch to:#mm |
|
794 |
UnitConverter convert:1 from:#inch to:#millimeter |
|
795 |
UnitConverter convert:1 from:#inch to:#nanometer |
|
796 |
UnitConverter convert:1 from:#mm to:#km |
|
797 |
UnitConverter convert:1 from:#km to:#foot |
|
798 |
||
799 |
indirect: |
|
800 |
UnitConverter convert:1 from:#mm to:#twip |
|
801 |
UnitConverter convert:1 from:#inch to:#twip |
|
369 | 802 |
UnitConverter convert:1 from:'letterH' to:#point |
803 |
UnitConverter convert:1 from:'letterlH' to:#point |
|
364 | 804 |
|
805 |
UnitConverter convert:5 from:#barrel to:#liter |
|
806 |
UnitConverter convert:10 from:#kilogram to:#carat |
|
807 |
||
808 |
UnitConverter convert:1 from:#liter to:#floz |
|
363 | 809 |
" |
810 |
||
811 |
"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
|
812 |
"Modified: 6.8.1997 / 18:10:22 / cg" |
363 | 813 |
! |
814 |
||
3573 | 815 |
fileSizeBinaryStringFor:nBytes |
3575 | 816 |
"return a useful string for a size-in-bytes in programmer's notation; |
3573 | 817 |
Scales by Ki (*1024), Mi (*1024*1024) or Gi (*1024*1024*1024) or even Ti |
818 |
as required and useful. |
|
819 |
Provided here since this is so common... |
|
820 |
Notice: the scale is 1024 |
|
821 |
the 1024 scale has been standardized in 1998 by IEC to MiB, GiB, etc. |
|
822 |
See https://en.wikipedia.org/wiki/Mebibyte" |
|
823 |
||
824 |
^ self |
|
825 |
unitStringFor:nBytes |
|
3574 | 826 |
scale:1024 |
3575 | 827 |
rounded:true |
828 |
unitStrings:#(' Byte' ' KiB' ' MiB' ' GiB' ' TiB' ' PiB' ' EiB' ' ZiB' ' YiB') |
|
3573 | 829 |
|
830 |
"/ the one below was written to save divisions. |
|
831 |
"/ It seems to be not worth the effort... |
|
832 |
||
833 |
"/ |unitString n| |
|
834 |
"/ |
|
835 |
"/ nBytes isNil ifTrue:[ |
|
836 |
"/ ^ '-' |
|
837 |
"/ ]. |
|
838 |
"/ |
|
839 |
"/ nBytes < (1000 * 1024 * 1024 * 1024) ifTrue:[ |
|
840 |
"/ nBytes < (1000 * 1024 * 1024) ifTrue:[ |
|
841 |
"/ nBytes < (1000 * 1024) ifTrue:[ |
|
842 |
"/ nBytes < 1000 ifTrue:[ |
|
843 |
"/ n := nBytes. |
|
844 |
"/ unitString := ' '. |
|
845 |
"/ ] ifFalse:[ |
|
846 |
"/ n := (nBytes / 1024) asFixedPoint:1. |
|
847 |
"/ unitString := ' K' |
|
848 |
"/ ] |
|
849 |
"/ ] ifFalse:[ |
|
850 |
"/ n := (nBytes / (1024 * 1024)) asFixedPoint:1. |
|
851 |
"/ unitString := ' M' |
|
852 |
"/ ]. |
|
853 |
"/ ] ifFalse:[ |
|
854 |
"/ n := (nBytes / 1024 / (1024 * 1024)) asFixedPoint:1. |
|
855 |
"/ unitString := ' G' |
|
856 |
"/ ] |
|
857 |
"/ ] ifFalse:[ |
|
858 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
859 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
860 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
861 |
"/ unitString := ' E' |
|
862 |
"/ ] ifFalse:[ |
|
863 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / 1024) asFixedPoint:1. |
|
864 |
"/ unitString := ' P' |
|
865 |
"/ ] |
|
866 |
"/ ] ifFalse:[ |
|
867 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
868 |
"/ unitString := ' T' |
|
869 |
"/ ]. |
|
870 |
"/ ]. |
|
871 |
"/ |
|
872 |
"/"/ n truncated = n ifTrue:[ |
|
873 |
"/"/ n := n truncated |
|
874 |
"/"/ ]. |
|
875 |
"/ ^ n printString , unitString. |
|
876 |
||
877 |
" |
|
878 |
self fileSizeStringFor:nil |
|
879 |
self fileSizeStringFor:10 |
|
880 |
self fileSizeStringFor:100 |
|
881 |
self fileSizeStringFor:1000 |
|
882 |
self fileSizeStringFor:8192 |
|
883 |
self fileSizeStringFor:10000 |
|
884 |
self fileSizeStringFor:100000 |
|
885 |
self fileSizeStringFor:1000000 |
|
886 |
self fileSizeStringFor:10000000 |
|
887 |
self fileSizeStringFor:100000000 |
|
888 |
self fileSizeStringFor:1000000000 |
|
889 |
self fileSizeStringFor:10000000000 |
|
890 |
self fileSizeStringFor:100000000000 |
|
891 |
self fileSizeStringFor:1000000000000 |
|
892 |
self fileSizeStringFor:10000000000000 |
|
893 |
self fileSizeStringFor:10000000000000000 |
|
894 |
self fileSizeStringFor:100000000000000000000 |
|
895 |
self fileSizeStringFor:1000000000000000000000 |
|
896 |
" |
|
897 |
||
898 |
"Modified: / 17-12-2013 / 16:55:17 / cg" |
|
899 |
"Modified (comment): / 18-12-2014 / 09:54:33 / sr" |
|
900 |
! |
|
901 |
||
1292 | 902 |
fileSizeFromString:fileSizeString |
3573 | 903 |
"given a fileSize string (i.e. a number followed by optional kB, MB, GB etc. or KiB, MiB, GiB,...). |
904 |
Notice that in 1998, IEC standardized the scales to 1000 for k,M,G,... and 1024 for Ki, Mi, Gi, etc. |
|
905 |
See https://en.wikipedia.org/wiki/Mebibyte. |
|
1292 | 906 |
Provided here since this is so common..." |
907 |
||
3573 | 908 |
|numericValue s scaleChar factor scale| |
1292 | 909 |
|
910 |
fileSizeString isEmpty ifTrue:[ |
|
911 |
^ 0 |
|
912 |
]. |
|
913 |
||
914 |
scale := 1. |
|
915 |
s := fileSizeString readStream. |
|
916 |
numericValue := Number readFrom:s onError:[0]. |
|
917 |
s skipSeparators. |
|
918 |
s atEnd ifFalse:[ |
|
919 |
scaleChar := s next asLowercase. |
|
3573 | 920 |
factor := 1000. |
921 |
s peek == $i ifTrue:[ |
|
922 |
factor := 1024 |
|
923 |
]. |
|
3486 | 924 |
scaleChar == $k ifTrue:[ |
3573 | 925 |
scale := factor |
1292 | 926 |
] ifFalse:[ |
3486 | 927 |
scaleChar == $m ifTrue:[ |
3573 | 928 |
scale := factor * factor |
1292 | 929 |
] ifFalse:[ |
3486 | 930 |
scaleChar == $g ifTrue:[ |
3573 | 931 |
scale := factor * factor * factor |
1292 | 932 |
] ifFalse:[ |
3486 | 933 |
scaleChar == $t ifTrue:[ |
3573 | 934 |
scale := factor * factor * factor * factor |
3142 | 935 |
] ifFalse:[ |
3486 | 936 |
scaleChar == $p ifTrue:[ |
3573 | 937 |
scale := factor * factor * factor * factor * factor |
3142 | 938 |
] ifFalse:[ |
3486 | 939 |
scaleChar == $e ifTrue:[ |
3573 | 940 |
scale := factor * factor * factor * factor * factor * factor |
941 |
] ifFalse:[ |
|
942 |
scaleChar == $z ifTrue:[ |
|
943 |
scale := factor * factor * factor * factor * factor * factor * factor |
|
944 |
] ifFalse:[ |
|
945 |
scaleChar == $y ifTrue:[ |
|
946 |
scale := factor * factor * factor * factor * factor * factor * factor * factor |
|
947 |
] |
|
948 |
] |
|
3142 | 949 |
] |
950 |
] |
|
1292 | 951 |
] |
952 |
] |
|
953 |
] |
|
954 |
]. |
|
955 |
]. |
|
956 |
^ (numericValue*scale) asInteger |
|
957 |
||
958 |
" |
|
959 |
self fileSizeFromString:'1234' |
|
1295 | 960 |
self fileSizeFromString:'10 k' |
3573 | 961 |
self fileSizeFromString:'10 kB' |
962 |
self fileSizeFromString:'10 KiB' |
|
1295 | 963 |
self fileSizeFromString:'0.6 k' |
964 |
self fileSizeFromString:'1.8 M' |
|
3573 | 965 |
self fileSizeFromString:'1.8 MB' |
966 |
self fileSizeFromString:'1.8 MiB' |
|
1295 | 967 |
self fileSizeFromString:'0.5 G' |
968 |
self fileSizeFromString:'0.5 T' |
|
3142 | 969 |
self fileSizeFromString:'0.5 P' |
970 |
self fileSizeFromString:'0.5 E' |
|
1292 | 971 |
" |
3142 | 972 |
|
973 |
"Modified (comment): / 17-12-2013 / 16:56:56 / cg" |
|
1292 | 974 |
! |
975 |
||
3575 | 976 |
fileSizeSIStringFor:nBytes |
977 |
"return a useful string for a size-in-bytes in SI standard notation; |
|
3573 | 978 |
Scales by K (*1000), M (*1000*1000) or G (*1000*1000*1000) or even T |
1294 | 979 |
as required and useful. |
3573 | 980 |
Provided here since this is so common... |
981 |
Notice: the scale is 1000 |
|
982 |
the 1000 scale has been standardized in 1998 by IEC to MB, GB, etc. |
|
983 |
See https://en.wikipedia.org/wiki/Mebibyte" |
|
1143 | 984 |
|
3573 | 985 |
^ self |
986 |
unitStringFor:nBytes |
|
987 |
scale:1000 |
|
3575 | 988 |
rounded:true |
989 |
unitStrings:#(' Byte' ' KB' ' MB' ' GB' ' TB' ' PB' ' EB' ' ZB' ' YB') |
|
990 |
||
991 |
"/ |unitString n| |
|
992 |
"/ |
|
993 |
"/ nBytes isNil ifTrue:[ |
|
994 |
"/ ^ '-' |
|
995 |
"/ ]. |
|
996 |
"/ |
|
997 |
"/ nBytes < (1000 * 1024 * 1024 * 1024) ifTrue:[ |
|
998 |
"/ nBytes < (1000 * 1024 * 1024) ifTrue:[ |
|
999 |
"/ nBytes < (1000 * 1024) ifTrue:[ |
|
1000 |
"/ nBytes < 1000 ifTrue:[ |
|
1001 |
"/ n := nBytes. |
|
1002 |
"/ unitString := ' '. |
|
1003 |
"/ ] ifFalse:[ |
|
1004 |
"/ n := (nBytes / 1024) asFixedPoint:1. |
|
1005 |
"/ unitString := ' K' |
|
1006 |
"/ ] |
|
1007 |
"/ ] ifFalse:[ |
|
1008 |
"/ n := (nBytes / (1024 * 1024)) asFixedPoint:1. |
|
1009 |
"/ unitString := ' M' |
|
1010 |
"/ ]. |
|
1011 |
"/ ] ifFalse:[ |
|
1012 |
"/ n := (nBytes / 1024 / (1024 * 1024)) asFixedPoint:1. |
|
1013 |
"/ unitString := ' G' |
|
1014 |
"/ ] |
|
1015 |
"/ ] ifFalse:[ |
|
1016 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
1017 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
1018 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
1019 |
"/ unitString := ' E' |
|
1020 |
"/ ] ifFalse:[ |
|
1021 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / 1024) asFixedPoint:1. |
|
1022 |
"/ unitString := ' P' |
|
1023 |
"/ ] |
|
1024 |
"/ ] ifFalse:[ |
|
1025 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
1026 |
"/ unitString := ' T' |
|
1027 |
"/ ]. |
|
1028 |
"/ ]. |
|
1029 |
"/ |
|
1030 |
"/"/ n truncated = n ifTrue:[ |
|
1031 |
"/"/ n := n truncated |
|
1032 |
"/"/ ]. |
|
1033 |
"/ ^ n printString , unitString. |
|
1034 |
||
1035 |
" |
|
1036 |
self fileSizeStringFor:nil |
|
1037 |
self fileSizeStringFor:10 |
|
1038 |
self fileSizeStringFor:100 |
|
1039 |
self fileSizeStringFor:1000 |
|
1040 |
self fileSizeStringFor:8192 |
|
1041 |
self fileSizeStringFor:10000 |
|
1042 |
self fileSizeStringFor:100000 |
|
1043 |
self fileSizeStringFor:1000000 |
|
1044 |
self fileSizeStringFor:10000000 |
|
1045 |
self fileSizeStringFor:100000000 |
|
1046 |
self fileSizeStringFor:1000000000 |
|
1047 |
self fileSizeStringFor:10000000000 |
|
1048 |
self fileSizeStringFor:100000000000 |
|
1049 |
self fileSizeStringFor:1000000000000 |
|
1050 |
self fileSizeStringFor:10000000000000 |
|
1051 |
self fileSizeStringFor:10000000000000000 |
|
1052 |
self fileSizeStringFor:100000000000000000000 |
|
1053 |
self fileSizeStringFor:1000000000000000000000 |
|
1054 |
" |
|
1055 |
||
1056 |
"Modified: / 17-12-2013 / 16:55:17 / cg" |
|
1057 |
"Modified (comment): / 18-12-2014 / 09:54:33 / sr" |
|
1058 |
! |
|
1059 |
||
1060 |
fileSizeStringFor:nBytes |
|
1061 |
"return a useful string for a size-in-bytes. |
|
1062 |
Scales either by 1000 or 1024, depending on the user preferences. |
|
1063 |
See https://en.wikipedia.org/wiki/Mebibyte" |
|
1064 |
||
1065 |
^ self |
|
1066 |
unitStringFor:nBytes |
|
1067 |
scale:(UserPreferences current unitForFileSize) |
|
1068 |
rounded:true |
|
1069 |
unitStrings:(UserPreferences current unitStringsForFileSize) |
|
3457 | 1070 |
|
3573 | 1071 |
"/ |unitString n| |
1072 |
"/ |
|
1073 |
"/ nBytes isNil ifTrue:[ |
|
1074 |
"/ ^ '-' |
|
1346 | 1075 |
"/ ]. |
3573 | 1076 |
"/ |
1077 |
"/ nBytes < (1000 * 1024 * 1024 * 1024) ifTrue:[ |
|
1078 |
"/ nBytes < (1000 * 1024 * 1024) ifTrue:[ |
|
1079 |
"/ nBytes < (1000 * 1024) ifTrue:[ |
|
1080 |
"/ nBytes < 1000 ifTrue:[ |
|
1081 |
"/ n := nBytes. |
|
1082 |
"/ unitString := ' '. |
|
1083 |
"/ ] ifFalse:[ |
|
1084 |
"/ n := (nBytes / 1024) asFixedPoint:1. |
|
1085 |
"/ unitString := ' K' |
|
1086 |
"/ ] |
|
1087 |
"/ ] ifFalse:[ |
|
1088 |
"/ n := (nBytes / (1024 * 1024)) asFixedPoint:1. |
|
1089 |
"/ unitString := ' M' |
|
1090 |
"/ ]. |
|
1091 |
"/ ] ifFalse:[ |
|
1092 |
"/ n := (nBytes / 1024 / (1024 * 1024)) asFixedPoint:1. |
|
1093 |
"/ unitString := ' G' |
|
1094 |
"/ ] |
|
1095 |
"/ ] ifFalse:[ |
|
1096 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
1097 |
"/ nBytes >= (1000 * 1024 * 1024 * 1024 * 1024 * 1024) ifTrue:[ |
|
1098 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
1099 |
"/ unitString := ' E' |
|
1100 |
"/ ] ifFalse:[ |
|
1101 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024) / 1024) asFixedPoint:1. |
|
1102 |
"/ unitString := ' P' |
|
1103 |
"/ ] |
|
1104 |
"/ ] ifFalse:[ |
|
1105 |
"/ n := (nBytes / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
|
1106 |
"/ unitString := ' T' |
|
1107 |
"/ ]. |
|
1108 |
"/ ]. |
|
1109 |
"/ |
|
1110 |
"/"/ n truncated = n ifTrue:[ |
|
1111 |
"/"/ n := n truncated |
|
1112 |
"/"/ ]. |
|
1113 |
"/ ^ n printString , unitString. |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1114 |
|
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1115 |
" |
3457 | 1116 |
self fileSizeStringFor:nil |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1117 |
self fileSizeStringFor:10 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1118 |
self fileSizeStringFor:100 |
1298
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1119 |
self fileSizeStringFor:1000 |
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1120 |
self fileSizeStringFor:8192 |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1121 |
self fileSizeStringFor:10000 |
1298
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1122 |
self fileSizeStringFor:100000 |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1123 |
self fileSizeStringFor:1000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1124 |
self fileSizeStringFor:10000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1125 |
self fileSizeStringFor:100000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1126 |
self fileSizeStringFor:1000000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1127 |
self fileSizeStringFor:10000000000 |
1293 | 1128 |
self fileSizeStringFor:100000000000 |
1129 |
self fileSizeStringFor:1000000000000 |
|
1130 |
self fileSizeStringFor:10000000000000 |
|
3141 | 1131 |
self fileSizeStringFor:10000000000000000 |
3142 | 1132 |
self fileSizeStringFor:100000000000000000000 |
1133 |
self fileSizeStringFor:1000000000000000000000 |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1134 |
" |
3141 | 1135 |
|
3142 | 1136 |
"Modified: / 17-12-2013 / 16:55:17 / cg" |
3457 | 1137 |
"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
|
1138 |
! |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
1139 |
|
3120 | 1140 |
frequencyFromString:frequencyString |
1141 |
"given a frequency string (i.e. a number followed by optional kHz, Mhz or Hhz as required. |
|
1142 |
Provided here since this is so common..." |
|
1143 |
||
1144 |
|numericValue s scaleChar scale rest| |
|
1145 |
||
1146 |
frequencyString isEmpty ifTrue:[ |
|
1147 |
^ 0 |
|
1148 |
]. |
|
1149 |
||
1150 |
scale := 1. |
|
1151 |
s := frequencyString readStream. |
|
1152 |
numericValue := Number readFrom:s onError:[0]. |
|
1153 |
s skipSeparators. |
|
1154 |
s atEnd ifFalse:[ |
|
1155 |
scaleChar := s next asLowercase. |
|
3486 | 1156 |
scaleChar == $/ ifTrue:[ |
3120 | 1157 |
rest := s upToSeparator asLowercase. |
1158 |
(rest = 'm' or:[rest = 'min']) ifTrue:[ |
|
1159 |
scale := 1/60 |
|
1160 |
] ifFalse:[ |
|
1161 |
(rest = 'h' or:[rest = 'hr' or:[rest = 'hour']]) ifTrue:[ |
|
1162 |
scale := 1/60/60 |
|
1163 |
] ifFalse:[ |
|
1164 |
(rest = 'd' or:[rest = 'dy' or:[rest = 'day']]) ifTrue:[ |
|
1165 |
scale := 1/60/60/24 |
|
1166 |
]. |
|
1167 |
]. |
|
1168 |
]. |
|
1169 |
] ifFalse:[ |
|
3486 | 1170 |
scaleChar == $k ifTrue:[ |
3120 | 1171 |
scale := 1000 |
1172 |
] ifFalse:[ |
|
3486 | 1173 |
scaleChar == $m ifTrue:[ |
3120 | 1174 |
scale := 1000 * 1000 |
1175 |
] ifFalse:[ |
|
3487 | 1176 |
scaleChar == $g ifTrue:[ |
3120 | 1177 |
scale := 1000 * 1000 * 1000 |
1178 |
] ifFalse:[ |
|
3487 | 1179 |
scaleChar == $t ifTrue:[ |
3120 | 1180 |
scale := 1000 * 1000 * 1000 * 1000 |
1181 |
] |
|
1182 |
] |
|
1183 |
] |
|
1184 |
]. |
|
1185 |
] |
|
1186 |
]. |
|
1187 |
^ (numericValue*scale) |
|
1188 |
||
1189 |
" |
|
1190 |
self frequencyFromString:'1234' |
|
1191 |
self frequencyFromString:'10 kHz' |
|
1192 |
self frequencyFromString:'0.6 k' |
|
1193 |
self frequencyFromString:'1.8 Mhz' |
|
1194 |
self frequencyFromString:'0.5 Ghz' |
|
1195 |
self frequencyFromString:'0.5 Thz' |
|
1196 |
self frequencyFromString:'10/s' |
|
1197 |
self frequencyFromString:'10/min' |
|
1198 |
self frequencyFromString:'10/hr' |
|
1199 |
" |
|
1200 |
! |
|
1201 |
||
1202 |
frequencyStringFor:hertz |
|
1203 |
"return a useful string for a frequency; |
|
1204 |
Scales by K (*1000), M (*1000*1000) or G (*1000*1000*1000) or even T |
|
1205 |
as required and useful. |
|
1206 |
Provided here since this is so common..." |
|
1207 |
||
3573 | 1208 |
^ self |
1209 |
unitStringFor:hertz |
|
1210 |
scale:1000 |
|
3574 | 1211 |
rounded:false |
3573 | 1212 |
unitStrings:#(' Hz' ' Khz' ' Mhz' ' Ghz' ' Thz' ' Phz' ' Ehz') |
3120 | 1213 |
|
1214 |
" |
|
1215 |
self frequencyStringFor:10 |
|
1216 |
self frequencyStringFor:100 |
|
1217 |
self frequencyStringFor:1000 |
|
1218 |
self frequencyStringFor:8192 |
|
1219 |
self frequencyStringFor:10000 |
|
1220 |
self frequencyStringFor:100000 |
|
1221 |
self frequencyStringFor:1000000 |
|
1222 |
self frequencyStringFor:10000000 |
|
1223 |
self frequencyStringFor:100000000 |
|
1224 |
self frequencyStringFor:1000000000 |
|
1225 |
self frequencyStringFor:10000000000 |
|
1226 |
self frequencyStringFor:100000000000 |
|
1227 |
self frequencyStringFor:1000000000000 |
|
1228 |
self frequencyStringFor:10000000000000 |
|
1229 |
" |
|
1230 |
! |
|
1231 |
||
363 | 1232 |
inchToMillimeter:inches |
364 | 1233 |
"convert inches to mm. Made this an extra method, since its so common." |
363 | 1234 |
|
1235 |
^ inches * 25.4 |
|
1236 |
||
1237 |
"Created: 31.5.1996 / 13:37:31 / cg" |
|
364 | 1238 |
"Modified: 31.5.1996 / 18:08:14 / cg" |
363 | 1239 |
! |
1240 |
||
1241 |
millimeterToInch:mm |
|
364 | 1242 |
"convert mm to inches. Made this an extra method, since its so common." |
363 | 1243 |
|
1244 |
^ mm / 25.4 |
|
1245 |
||
1246 |
"Created: 31.5.1996 / 13:37:48 / cg" |
|
364 | 1247 |
"Modified: 31.5.1996 / 18:08:20 / cg" |
3573 | 1248 |
! |
1249 |
||
3574 | 1250 |
unitStringFor:nBytes scale:k rounded:rounded unitStrings:unitStrings |
3573 | 1251 |
"common helper for SI (1000-based) and binary (1024-based) human readable string generators. |
3574 | 1252 |
Notice that both forms are often encountered and useful. |
1253 |
If rounded is true, round to nearest integer" |
|
3573 | 1254 |
|
3575 | 1255 |
|n kN idx val| |
3573 | 1256 |
|
1257 |
nBytes isNil ifTrue:[ |
|
1258 |
^ '-' |
|
1259 |
]. |
|
1260 |
||
1261 |
idx := 1. |
|
1262 |
n := nBytes. |
|
1263 |
kN := 1. |
|
1264 |
[ (idx < unitStrings size) and:[ nBytes >= (kN * k) ] ] whileTrue:[ |
|
1265 |
idx := idx + 1. |
|
1266 |
kN := kN * k. |
|
1267 |
]. |
|
3575 | 1268 |
|
1269 |
val := nBytes / kN. |
|
1270 |
((rounded and:[val > 20]) or:[val isInteger]) ifTrue:[ |
|
1271 |
^ (val rounded) printString , (unitStrings at:idx) |
|
3574 | 1272 |
]. |
3575 | 1273 |
^ (val asFixedPoint:1) printString , (unitStrings at:idx) |
3573 | 1274 |
|
1275 |
" |
|
1276 |
self fileSizeStringFor:nil |
|
1277 |
self fileSizeStringFor:10 |
|
1278 |
self fileSizeStringFor:100 |
|
3574 | 1279 |
self fileSizeStringFor:900 |
1280 |
self fileSizeStringFor:1100 |
|
1281 |
self fileSizeStringFor:1400 |
|
1282 |
self fileSizeStringFor:1499 |
|
1283 |
self fileSizeStringFor:1500 |
|
1284 |
||
3573 | 1285 |
self fileSizeStringFor:1000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
1286 |
self fileSizeStringFor:8192 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1287 |
self fileSizeStringFor:10000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1288 |
self fileSizeStringFor:100000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1289 |
self fileSizeStringFor:1000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1290 |
self fileSizeStringFor:10000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1291 |
self fileSizeStringFor:100000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1292 |
self fileSizeStringFor:1000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1293 |
self fileSizeStringFor:10000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1294 |
self fileSizeStringFor:100000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1295 |
self fileSizeStringFor:1000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1296 |
self fileSizeStringFor:10000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1297 |
self fileSizeStringFor:10000000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1298 |
self fileSizeStringFor:100000000000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1299 |
self fileSizeStringFor:1000000000000000000000 scale:1000 unitStrings:#('' 'kB' 'MB' 'GB' 'TB' 'PB' 'EB' ) |
|
1300 |
||
1301 |
self fileSizeStringFor:8192 scale:1024 unitStrings:#('' 'KiB' 'MiB' 'GiB' 'TiB' 'PiB' 'EiB' ) |
|
1302 |
self fileSizeStringFor:81920000 scale:1024 unitStrings:#('' 'KiB' 'MiB' 'GiB' 'TiB' 'PiB' 'EiB' ) |
|
1303 |
" |
|
1304 |
||
1305 |
"Modified: / 17-12-2013 / 16:55:17 / cg" |
|
1306 |
"Modified (comment): / 18-12-2014 / 09:54:33 / sr" |
|
363 | 1307 |
! ! |
1308 |
||
516 | 1309 |
!UnitConverter class methodsFor:'missing conversion'! |
1310 |
||
1311 |
noConversionFrom:sourceUnit to:destUnit |
|
1312 |
Transcript showCR:'no conversion ' , sourceUnit printString , ' -> ' , destUnit printString. |
|
1313 |
^ nil |
|
1314 |
||
1315 |
"Created: 29.3.1997 / 17:56:03 / cg" |
|
1316 |
! ! |
|
1317 |
||
363 | 1318 |
!UnitConverter class methodsFor:'private'! |
1319 |
||
1320 |
addConversion:factor from:sourceMetric to:destMetric |
|
1321 |
"add a conversion" |
|
1322 |
||
1323 |
|conversion| |
|
1324 |
||
1325 |
conversion := Conversions at:sourceMetric ifAbsent:nil. |
|
1326 |
conversion isNil ifTrue:[ |
|
601 | 1327 |
conversion := IdentityDictionary new. |
1328 |
Conversions at:sourceMetric put:conversion |
|
363 | 1329 |
]. |
1330 |
conversion at:destMetric put:factor. |
|
1331 |
^ conversion |
|
1332 |
||
1333 |
"Created: 31.5.1996 / 13:51:25 / cg" |
|
515 | 1334 |
"Modified: 29.3.1997 / 17:19:31 / cg" |
363 | 1335 |
! |
1336 |
||
1337 |
convertDirect:howMany from:sourceMetric to:destMetric |
|
1338 |
"given a value in sourceMetric (symbolic), try to convert it |
|
1339 |
to destMetric (symbolic); |
|
1340 |
Only direct conversions are tried; return nil, if the conversion is unknown." |
|
1341 |
||
1342 |
|conversion factor| |
|
1343 |
||
550 | 1344 |
Conversions isNil ifTrue:[ |
601 | 1345 |
self initializeConversions |
550 | 1346 |
]. |
1347 |
||
363 | 1348 |
conversion := Conversions at:sourceMetric ifAbsent:nil. |
1349 |
conversion isNil ifTrue:[^ nil]. |
|
1350 |
||
1351 |
factor := conversion at:destMetric ifAbsent:nil. |
|
1352 |
factor isNil ifTrue:[^ nil]. |
|
1353 |
||
364 | 1354 |
factor isNumber ifTrue:[ |
601 | 1355 |
^ howMany * factor |
364 | 1356 |
]. |
515 | 1357 |
^ factor value:howMany |
363 | 1358 |
|
1359 |
" |
|
1360 |
UnitConverter convertDirect:1 from:#inch to:#meter |
|
1361 |
UnitConverter convertDirect:1 from:#inch to:#millimeter |
|
515 | 1362 |
UnitConverter convertDirect:1 from:#degrees to:#fahrenheit |
363 | 1363 |
" |
1364 |
||
1365 |
"Created: 31.5.1996 / 13:54:33 / cg" |
|
550 | 1366 |
"Modified: 22.7.1997 / 13:57:52 / cg" |
363 | 1367 |
! |
1368 |
||
1369 |
unscaled:what |
|
1370 |
"given a unit, return the base and a factor as assoc, |
|
1371 |
or nil if not found" |
|
1372 |
||
550 | 1373 |
Conversions isNil ifTrue:[ |
2995 | 1374 |
self initializeConversions |
550 | 1375 |
]. |
1376 |
||
363 | 1377 |
Scaling keysAndValuesDo:[:name :factor | |
2995 | 1378 |
|rest| |
363 | 1379 |
|
2995 | 1380 |
(what startsWith:name) ifTrue:[ |
1381 |
rest := what withoutPrefix:name. |
|
1382 |
rest := rest asSymbolIfInterned. |
|
1383 |
rest notNil ifTrue:[ |
|
1384 |
^ rest -> factor. |
|
1385 |
] |
|
1386 |
]. |
|
363 | 1387 |
]. |
1388 |
^ nil |
|
1389 |
||
1390 |
" |
|
1391 |
UnitConverter unscaled:#millimeter |
|
1392 |
UnitConverter unscaled:#nanometer |
|
1393 |
UnitConverter unscaled:#kilometer |
|
1394 |
UnitConverter unscaled:#fuzzymeter |
|
1395 |
" |
|
1396 |
||
550 | 1397 |
"Modified: 22.7.1997 / 13:57:46 / cg" |
363 | 1398 |
! ! |
1399 |
||
1400 |
!UnitConverter class methodsFor:'documentation'! |
|
1401 |
||
1402 |
version |
|
3573 | 1403 |
^ '$Header$' |
2320 | 1404 |
! |
1405 |
||
1406 |
version_CVS |
|
3573 | 1407 |
^ '$Header$' |
363 | 1408 |
! ! |
2988
f78fe0eb5d57
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
2547
diff
changeset
|
1409 |