author | Claus Gittinger <cg@exept.de> |
Tue, 27 Oct 2009 13:19:01 +0100 | |
changeset 2320 | c7f52f534977 |
parent 1457 | 6831feb0bd08 |
child 2443 | e64a7f2f3fb4 |
permissions | -rw-r--r-- |
884 | 1 |
" |
2 |
COPYRIGHT (c) 1996 eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
981 | 12 |
"{ Package: 'stx:libbasic2' }" |
13 |
||
363 | 14 |
Object subclass:#UnitConverter |
15 |
instanceVariableNames:'' |
|
516 | 16 |
classVariableNames:'Conversions Scaling Aliases Constants' |
363 | 17 |
poolDictionaries:'' |
18 |
category:'Magnitude-General' |
|
19 |
! |
|
20 |
||
21 |
!UnitConverter class methodsFor:'documentation'! |
|
22 |
||
884 | 23 |
copyright |
24 |
" |
|
25 |
COPYRIGHT (c) 1996 eXept Software AG |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
||
36 |
||
37 |
! |
|
38 |
||
363 | 39 |
documentation |
40 |
" |
|
41 |
In order to collect various unit conversions into one central |
|
42 |
manageable (and especially: browsable) place, all previously |
|
564 | 43 |
spread knowledge about metric (and other) conversions has been |
363 | 44 |
brought into this class. |
45 |
||
46 |
This class is purely functional, abstract and has no instances; |
|
47 |
all queries are via class protocol. |
|
48 |
Choosing the Magnitudes category as its home is arbitrary. |
|
49 |
||
515 | 50 |
The supported units are setup in the classes initialize method; |
364 | 51 |
supported are: |
886 | 52 |
meter - inch - feet - foot - yard |
53 |
meter2 - acre - 'german-ar' 'german-hektar' are |
|
54 |
liter - gallon - barrel - floz |
|
55 |
celsius - fahrenheit |
|
364 | 56 |
and many others. |
57 |
||
515 | 58 |
The converter does a recursive search for a conversion; |
59 |
thus, if there is a conversion from #inch to #millimeter and another |
|
60 |
from #millimeter to #kilometer, conversion from #inch to #kilometer |
|
61 |
is found automatically. |
|
62 |
||
886 | 63 |
No Warranty: |
64 |
The numbers and conversion factors were obtained from the Unix |
|
65 |
units command. Please check before using it - there might be |
|
66 |
typos or wrong conversions in the setup code. |
|
67 |
||
68 |
||
1323 | 69 |
Notice: |
70 |
This class is *much* older than the younger and more object oriented |
|
71 |
Measurements package. Other than solving similar problems, these two packages |
|
72 |
are not related. |
|
73 |
||
74 |
||
363 | 75 |
[author:] |
886 | 76 |
Claus Gittinger |
363 | 77 |
|
78 |
see also: |
|
886 | 79 |
examples |
80 |
/usr/share/lib/unittab (if present on your system) |
|
1323 | 81 |
Measurement Unit |
363 | 82 |
" |
83 |
! |
|
84 |
||
85 |
examples |
|
86 |
" |
|
87 |
ever wanted to know, how many floz's are there in a european |
|
88 |
Coce bottle ? |
|
982 | 89 |
[exBegin] |
90 |
Transcript showCR: |
|
91 |
(UnitConverter convert:1 from:#liter to:#floz) |
|
92 |
[exEnd] |
|
93 |
||
94 |
[exBegin] |
|
95 |
Transcript showCR: |
|
96 |
(UnitConverter convert:43.5 from:#oz to:#gram) |
|
97 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
98 |
|
363 | 99 |
or, how many square-meters an acre is: |
982 | 100 |
[exBegin] |
101 |
Transcript showCR: |
|
102 |
(UnitConverter convert:1 from:#acre to:#'meter^2') |
|
103 |
[exEnd] |
|
363 | 104 |
|
516 | 105 |
|
515 | 106 |
or europeans might want to know, what those |
107 |
fahrenheit numbers mean in an US weather report ;-): |
|
982 | 108 |
[exBegin] |
109 |
Transcript showCR:( |
|
110 |
(UnitConverter convert:80 from:#fahrenheit to:#celsius) |
|
111 |
asFixedPoint:1) |
|
112 |
[exEnd] |
|
515 | 113 |
|
516 | 114 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
115 |
how fast do I drive ? :-) |
982 | 116 |
[exBegin] |
117 |
Transcript showCR:( |
|
118 |
UnitConverter convert:200 from:#'km/hr' to:#'mile/hr') |
|
119 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
120 |
how fast does Chris drive ? :-) |
982 | 121 |
[exBegin] |
122 |
Transcript showCR:( |
|
123 |
UnitConverter convert:65 from:#'mile/hr' to:#'km/hr') |
|
124 |
[exEnd] |
|
516 | 125 |
|
126 |
||
127 |
calories or joule ? |
|
982 | 128 |
[exBegin] |
129 |
Transcript showCR:( |
|
130 |
UnitConverter convert:0.18 from:#'kilocalorie' to:#'kilojoule') |
|
131 |
[exEnd] |
|
132 |
[exBegin] |
|
133 |
Transcript showCR:( |
|
134 |
UnitConverter convert:2000 from:#'kilocalorie' to:#'kilojoule') |
|
135 |
[exEnd] |
|
516 | 136 |
|
137 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
138 |
distances: |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
139 |
|
982 | 140 |
[exBegin] |
141 |
Transcript showCR:( |
|
142 |
UnitConverter convert:1 from:#'lightsecond' to:#'kilometer') |
|
143 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
144 |
thats the same: |
982 | 145 |
[exBegin] |
146 |
Transcript showCR:( |
|
147 |
UnitConverter convert:1 from:#'lightspeed*s' to:#'kilometer') |
|
148 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
149 |
a days travel ... |
982 | 150 |
[exBegin] |
151 |
Transcript showCR:( |
|
152 |
UnitConverter convert:1 from:#'lightspeed*dy' to:#'kilometer') |
|
153 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
154 |
a year travel ... |
982 | 155 |
[exBegin] |
156 |
Transcript showCR:( |
|
157 |
UnitConverter convert:1 from:#'lightyear' to:#'kilometer') |
|
158 |
[exEnd] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
159 |
|
2320 | 160 |
wonna race on the autobahn ? |
161 |
[exBegin] |
|
162 |
Transcript showCR:( |
|
163 |
UnitConverter convert:200 from:#'km/h' to:#'mph') |
|
164 |
[exEnd] |
|
165 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
166 |
|
363 | 167 |
real estate buyers might want to know, how many acres |
168 |
a german ar is: |
|
982 | 169 |
[exBegin] |
170 |
Transcript showCR: |
|
171 |
(UnitConverter convert:1 from:#'german-ar' to:#acre) |
|
172 |
[exEnd] |
|
516 | 173 |
- or how many square-feet are there in your living room: |
982 | 174 |
[exBegin] |
175 |
Transcript showCR: |
|
176 |
(UnitConverter convert:100 from:#'meter^2' to:#'foot^2') |
|
177 |
[exEnd] |
|
1031 | 178 |
- or how about: |
179 |
[exBegin] |
|
180 |
Transcript showCR: |
|
181 |
(UnitConverter convert:3600 from:#'ft*ft' to:#'m*,') |
|
182 |
[exEnd] |
|
183 |
||
516 | 184 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
185 |
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
|
186 |
(roughly, since a teaspoon is not a standard unit) |
982 | 187 |
[exBegin] |
188 |
Transcript showCR: |
|
189 |
(UnitConverter convert:1 from:#'meter^3' to:#teaspoon) |
|
190 |
[exEnd] |
|
365 | 191 |
how wide is a US page in inches: |
982 | 192 |
[exBegin] |
193 |
Transcript showCR: |
|
194 |
(UnitConverter convert:1 from:#'letterW' to:#inch) |
|
195 |
[exEnd] |
|
516 | 196 |
- in millimeter: |
982 | 197 |
[exBegin] |
198 |
Transcript showCR: |
|
199 |
(UnitConverter convert:1 from:#'letterW' to:#mm) |
|
200 |
[exEnd] |
|
516 | 201 |
|
202 |
||
365 | 203 |
the height of a US page in inches: |
982 | 204 |
[exBegin] |
205 |
Transcript showCR: |
|
206 |
(UnitConverter convert:1 from:#'letterH' to:#inch) |
|
207 |
[exEnd] |
|
516 | 208 |
- in millimeter: |
982 | 209 |
[exBegin] |
210 |
Transcript showCR: |
|
211 |
(UnitConverter convert:1 from:#'letterH' to:#mm) |
|
212 |
[exEnd] |
|
516 | 213 |
|
214 |
||
365 | 215 |
the same for european A4 standard page: |
982 | 216 |
[exBegin] |
217 |
Transcript showCR: |
|
218 |
(UnitConverter convert:1 from:#'a4H' to:#mm) |
|
219 |
[exEnd] |
|
220 |
[exBegin] |
|
221 |
Transcript showCR: |
|
222 |
(UnitConverter convert:1 from:#'a4H' to:#inch) |
|
223 |
[exEnd] |
|
363 | 224 |
" |
225 |
! ! |
|
226 |
||
227 |
!UnitConverter class methodsFor:'initialization'! |
|
228 |
||
550 | 229 |
initializeConversions |
363 | 230 |
"initialize common conversions" |
231 |
||
1135 | 232 |
self initializeScaleFactors. |
233 |
||
363 | 234 |
Conversions := IdentityDictionary new. |
235 |
Aliases := IdentityDictionary new. |
|
516 | 236 |
Constants := IdentityDictionary new. |
363 | 237 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
238 |
"/ ---------- velocity ------------- |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
239 |
|
556 | 240 |
Constants at:#lightspeed put:#(2.997925E8 #'m/s'). |
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
241 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
242 |
|
363 | 243 |
"/ -------------- length ------------- |
244 |
||
245 |
Aliases at:#km put:#kilometer. |
|
246 |
Aliases at:#m put:#meter. |
|
247 |
Aliases at:#dm put:#decimeter. |
|
248 |
Aliases at:#cm put:#centimeter. |
|
249 |
Aliases at:#mm put:#millimeter. |
|
250 |
Aliases at:#micron put:#micrometer. |
|
251 |
Aliases at:#nm put:#nanometer. |
|
252 |
Aliases at:#angstrom put:#decinanometer. |
|
253 |
||
254 |
"/ US |
|
255 |
self addConversion:12 from:#foot to:#inch. |
|
256 |
self addConversion:3 from:#yard to:#foot. |
|
257 |
self addConversion:5280 from:#mile to:#foot. |
|
515 | 258 |
self addConversion:0.001 from:#inch to:#mil. |
363 | 259 |
|
2320 | 260 |
Aliases at:#feet put:#foot. |
261 |
Aliases at:#ft put:#foot. |
|
262 |
Aliases at:#yd put:#yard. |
|
263 |
Aliases at:#mi put:#mile. |
|
264 |
Aliases at:#mph put:#'mile/hr'. |
|
363 | 265 |
|
266 |
"/ inch to millimeter |
|
267 |
self addConversion:(25.4/1000) from:#inch to:#meter. |
|
268 |
||
365 | 269 |
"/ nautic |
270 |
self addConversion:1852 from:#'nautical-mile' to:#meter. |
|
271 |
||
556 | 272 |
Aliases at:#lightsecond put:#'lightspeed*s'. |
273 |
Aliases at:#lightyear put:#'lightspeed*yr'. |
|
274 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
275 |
self addConversion:((Constants at:#lightspeed) at:1) from:#'lightspeed*s' to:#'meter'. |
385 | 276 |
|
516 | 277 |
"/ ---------- time ------------- |
278 |
||
279 |
self addConversion:60 from:#min to:#s. |
|
280 |
self addConversion:60 from:#hr to:#min. |
|
281 |
self addConversion:24 from:#dy to:#hr. |
|
282 |
self addConversion:365.24219879 from:#yr to:#dy. |
|
283 |
||
2320 | 284 |
Aliases at:#h put:#'hr'. |
285 |
Aliases at:#d put:#'dy'. |
|
516 | 286 |
|
385 | 287 |
"/ ---------- printing ------------- |
1135 | 288 |
self initializePrintValues. |
365 | 289 |
|
1135 | 290 |
"/ ---------------- area ------------------- |
291 |
||
292 |
"/ US |
|
293 |
self addConversion:4840 from:#acre to:#'yard^2'. |
|
294 |
||
295 |
self addConversion:100 from:#are to:#'meter^2'. |
|
296 |
self addConversion:100 from:#hectare to:#are. |
|
297 |
||
298 |
"/ german area - add your countries, and return to me ... |
|
299 |
Aliases at:#'german-ar' put:#are. |
|
300 |
Aliases at:#'german-hektar' put:#hectare. |
|
301 |
||
302 |
||
303 |
"/ ---------------- liquid ---------------- |
|
304 |
||
305 |
self addConversion:231 from:#gallon to:#'inch^3'. |
|
306 |
self addConversion:(1/4) from:#quart to:#gallon. "/ well - at least here, |
|
307 |
"/ thats also 1/4th of a good wine ;-) |
|
308 |
self addConversion:(1/2) from:#pint to:#quart. |
|
309 |
self addConversion:(1/16) from:#floz to:#pint. |
|
310 |
self addConversion:(1/8) from:#fldr to:#floz. |
|
311 |
||
312 |
self addConversion:42 from:#barrel to:#gallon. |
|
313 |
self addConversion:35.23907 from:#bushel to:#liter. |
|
314 |
||
315 |
Aliases at:#cc put:#'cm^3'. |
|
316 |
Aliases at:#liter put:#kilocc. |
|
317 |
Aliases at:#ml put:#milliliter. |
|
318 |
Aliases at:#gal put:#gallon. |
|
319 |
Aliases at:#qt put:#quart. |
|
320 |
||
2320 | 321 |
"/ well, a pint of beer is not always: |
322 |
self addConversion:277.420 from:#'british-gallon' to:#'inch^3'. |
|
323 |
self addConversion:(1/4) from:#'british-quart' to:#'british-gallon'. |
|
324 |
self addConversion:(1/2) from:#'british-pint' to:#'british-quart'. |
|
325 |
self addConversion:(1/16) from:#'british-floz' to:#'british-pint'. |
|
1135 | 326 |
|
327 |
"/ ---------------- mass ---------------- |
|
328 |
||
329 |
self addConversion:28.35 from:#ounce to:#gram. |
|
330 |
self addConversion:453.59237 from:#lb to:#gram. |
|
331 |
self addConversion:205 from:#carat to:#milligram. |
|
332 |
||
333 |
Aliases at:#oz put:#ounce. |
|
334 |
Aliases at:#lbs put:#lb. |
|
335 |
||
336 |
Aliases at:#gm put:#gram. |
|
337 |
Aliases at:#kg put:#kilogram. |
|
338 |
||
339 |
"/ ---------------- energy ---------------- |
|
340 |
||
341 |
Aliases at:#cal put:#calorie. |
|
342 |
Aliases at:#nt put:#newton. |
|
343 |
Aliases at:#joule put:#'nt*m'. |
|
2320 | 344 |
Aliases at:#J put:#'joule'. |
345 |
Aliases at:#N put:#'newton'. |
|
346 |
||
347 |
Aliases at:#watt put:#'J/s'. |
|
1135 | 348 |
|
349 |
self addConversion:4.1868 from:#calorie to:#joule. |
|
350 |
||
351 |
||
352 |
"/ ---------------- cooking ---------------- |
|
353 |
||
354 |
self addConversion:4 from:#tablespoon to:#fldr. |
|
355 |
self addConversion:(4/3) from:#teaspoon to:#fldr. |
|
356 |
||
357 |
||
358 |
"/ ---------------- temperature ---------------- |
|
359 |
||
360 |
self addConversion:[:d | d * 1.8 + 32] from:#celsius to:#fahrenheit. |
|
361 |
self addConversion:[:f | f - 32 / 1.8] from:#fahrenheit to:#celsius. |
|
362 |
||
363 |
" |
|
364 |
Conversions := nil. |
|
365 |
UnitConverter initializeConversions |
|
366 |
" |
|
367 |
||
368 |
"Created: / 22.7.1997 / 13:56:40 / cg" |
|
369 |
"Modified: / 10.10.2001 / 14:39:04 / cg" |
|
370 |
! |
|
371 |
||
372 |
initializePrintValues |
|
363 | 373 |
"/ inch to (roughly) a typesetter point |
374 |
self addConversion:(1/72) from:#point to:#inch. |
|
375 |
||
376 |
"/ point to twips; 20 twips (as in Rich-Text-Format) make a point |
|
377 |
self addConversion:(1/20) from:#twip to:#point. |
|
378 |
||
365 | 379 |
"/ US paper |
380 |
self addConversion:11 from:#'letter-page-height' to:#inch. |
|
381 |
self addConversion:8.5 from:#'letter-page-width' to:#inch. |
|
382 |
self addConversion:8.5 from:#'letter-landscape-page-height' to:#inch. |
|
383 |
self addConversion:11 from:#'letter-landscape-page-width' to:#inch. |
|
384 |
||
385 | 385 |
self addConversion:14 from:#'legal-page-height' to:#inch. |
386 |
self addConversion:8.5 from:#'legal-page-width' to:#inch. |
|
387 |
self addConversion:8.5 from:#'legal-landscape-page-height' to:#inch. |
|
388 |
self addConversion:14 from:#'legal-landscape-page-width' to:#inch. |
|
389 |
||
390 |
self addConversion:11 from:#'ledger-page-height' to:#inch. |
|
391 |
self addConversion:17 from:#'ledger-page-width' to:#inch. |
|
392 |
self addConversion:17 from:#'ledger-landscape-page-height' to:#inch. |
|
393 |
self addConversion:11 from:#'ledger-landscape-page-width' to:#inch. |
|
394 |
||
365 | 395 |
"/ European paper |
396 |
self addConversion:840 from:#'a1-page-height' to:#millimeter. |
|
397 |
self addConversion:592 from:#'a1-page-width' to:#millimeter. |
|
398 |
self addConversion:592 from:#'a1-landscape-page-height' to:#millimeter. |
|
399 |
self addConversion:840 from:#'a1-landscape-page-width' to:#millimeter. |
|
400 |
||
401 |
self addConversion:592 from:#'a2-page-height' to:#millimeter. |
|
402 |
self addConversion:420 from:#'a2-page-width' to:#millimeter. |
|
403 |
self addConversion:420 from:#'a2-landscape-page-height' to:#millimeter. |
|
404 |
self addConversion:592 from:#'a2-landscape-page-width' to:#millimeter. |
|
405 |
||
406 |
self addConversion:420 from:#'a3-page-height' to:#millimeter. |
|
407 |
self addConversion:296 from:#'a3-page-width' to:#millimeter. |
|
408 |
self addConversion:296 from:#'a3-landscape-page-height' to:#millimeter. |
|
409 |
self addConversion:420 from:#'a3-landscape-page-width' to:#millimeter. |
|
410 |
||
411 |
self addConversion:296 from:#'a4-page-height' to:#millimeter. |
|
412 |
self addConversion:210 from:#'a4-page-width' to:#millimeter. |
|
413 |
self addConversion:210 from:#'a4-landscape-page-height' to:#millimeter. |
|
414 |
self addConversion:296 from:#'a4-landscape-page-width' to:#millimeter. |
|
415 |
||
416 |
self addConversion:210 from:#'a5-page-height' to:#millimeter. |
|
417 |
self addConversion:148 from:#'a5-page-width' to:#millimeter. |
|
418 |
self addConversion:148 from:#'a5-landscape-page-height' to:#millimeter. |
|
419 |
self addConversion:210 from:#'a5-landscape-page-width' to:#millimeter. |
|
420 |
||
421 |
self addConversion:148 from:#'a6-page-height' to:#millimeter. |
|
422 |
self addConversion:105 from:#'a6-page-width' to:#millimeter. |
|
423 |
self addConversion:105 from:#'a6-landscape-page-height' to:#millimeter. |
|
424 |
self addConversion:148 from:#'a6-landscape-page-width' to:#millimeter. |
|
425 |
||
385 | 426 |
self addConversion:257 from:#'b5-page-height' to:#millimeter. |
427 |
self addConversion:182 from:#'b5-page-width' to:#millimeter. |
|
428 |
self addConversion:182 from:#'b5-landscape-page-height' to:#millimeter. |
|
429 |
self addConversion:257 from:#'b5-landscape-page-width' to:#millimeter. |
|
430 |
||
431 |
||
365 | 432 |
Aliases at:#letterW put:#'letter-page-width'. |
433 |
Aliases at:#letterH put:#'letter-page-height'. |
|
385 | 434 |
Aliases at:#legalW put:#'legal-page-width'. |
435 |
Aliases at:#legalH put:#'legal-page-height'. |
|
436 |
Aliases at:#ledgerW put:#'ledger-page-width'. |
|
437 |
Aliases at:#ledgerH put:#'ledger-page-height'. |
|
365 | 438 |
Aliases at:#a1W put:#'a1-page-width'. |
439 |
Aliases at:#a1H put:#'a1-page-height'. |
|
440 |
Aliases at:#a2W put:#'a2-page-width'. |
|
441 |
Aliases at:#a2H put:#'a2-page-height'. |
|
442 |
Aliases at:#a3W put:#'a3-page-width'. |
|
443 |
Aliases at:#a3H put:#'a3-page-height'. |
|
444 |
Aliases at:#a4W put:#'a4-page-width'. |
|
445 |
Aliases at:#a4H put:#'a4-page-height'. |
|
446 |
Aliases at:#a5W put:#'a5-page-width'. |
|
447 |
Aliases at:#a5H put:#'a5-page-height'. |
|
448 |
Aliases at:#a6W put:#'a6-page-width'. |
|
449 |
Aliases at:#a6H put:#'a6-page-height'. |
|
385 | 450 |
Aliases at:#b5W put:#'b5-page-width'. |
451 |
Aliases at:#b5H put:#'b5-page-height'. |
|
365 | 452 |
|
453 |
Aliases at:#letterlW put:#'letter-landscape-page-width'. |
|
454 |
Aliases at:#letterlH put:#'letter-landscape-page-height'. |
|
385 | 455 |
Aliases at:#legallW put:#'legal-landscape-page-width'. |
456 |
Aliases at:#legallH put:#'legal-landscape-page-height'. |
|
457 |
Aliases at:#ledgerlW put:#'ledger-landscape-page-width'. |
|
458 |
Aliases at:#ledgerlH put:#'ledger-landscape-page-height'. |
|
365 | 459 |
Aliases at:#a1lW put:#'a1-landscape-page-width'. |
460 |
Aliases at:#a1lH put:#'a1-landscape-page-height'. |
|
461 |
Aliases at:#a2lW put:#'a2-landscape-page-width'. |
|
462 |
Aliases at:#a2lH put:#'a2-landscape-page-height'. |
|
463 |
Aliases at:#a3lW put:#'a3-landscape-page-width'. |
|
464 |
Aliases at:#a3lH put:#'a3-landscape-page-height'. |
|
465 |
Aliases at:#a4lW put:#'a4-landscape-page-width'. |
|
466 |
Aliases at:#a4lH put:#'a4-landscape-page-height'. |
|
467 |
Aliases at:#a5lW put:#'a5-landscape-page-width'. |
|
468 |
Aliases at:#a5lH put:#'a5-landscape-page-height'. |
|
469 |
Aliases at:#a6lW put:#'a6-landscape-page-width'. |
|
470 |
Aliases at:#a6lH put:#'a6-landscape-page-height'. |
|
385 | 471 |
Aliases at:#b5lW put:#'b5-landscape-page-width'. |
472 |
Aliases at:#b5lH put:#'b5-landscape-page-height'. |
|
1135 | 473 |
! |
363 | 474 |
|
1135 | 475 |
initializeScaleFactors |
476 |
Scaling := IdentityDictionary new. |
|
363 | 477 |
|
1285 | 478 |
Scaling at:#yotta put:1000000000000000000000000. "/ 10^24 |
479 |
Scaling at:#zetta put:1000000000000000000000. "/ 10^21 |
|
480 |
Scaling at:#exa put:1000000000000000000. "/ 10^18 |
|
481 |
Scaling at:#peta put:1000000000000000. "/ 10^15 |
|
482 |
Scaling at:#tera put:1000000000000. "/ 10^12 |
|
483 |
Scaling at:#giga put:1000000000. "/ 10^9 |
|
484 |
Scaling at:#mega put:1000000. "/ 10^6 |
|
1135 | 485 |
Scaling at:#myria put:10000. |
1285 | 486 |
Scaling at:#kilo put:1000. "/ 10^3 |
487 |
Scaling at:#hecto put:100. "/ 10^2 |
|
488 |
Scaling at:#deca put:10. "/ 10^1 |
|
2320 | 489 |
Scaling at:#deka put:10. "/ 10^1 |
1323 | 490 |
Scaling at:#deci put:(1/10). "/ 10^-1 |
1135 | 491 |
Scaling at:#centi put:(1/100). |
1323 | 492 |
Scaling at:#milli put:(1/1000). "/ 10^-3 |
493 |
Scaling at:#micro put:(1/1000000). "/ 10^-6 |
|
494 |
Scaling at:#nano put:(1/1000000000). "/ 10^-9 |
|
495 |
Scaling at:#pico put:(1/1000000000000). "/ 10^-12 |
|
496 |
Scaling at:#femto put:(1/1000000000000000). "/ 10^-15 |
|
497 |
Scaling at:#atto put:(1/1000000000000000000). "/ 10^-18 |
|
498 |
Scaling at:#zepto put:(1/1000000000000000000000). "/ 10^-21 |
|
499 |
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
|
500 |
! ! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
501 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
502 |
!UnitConverter class methodsFor:'accessing'! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
503 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
504 |
scalings |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
505 |
"return the set of known scaling prefixes" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
506 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
507 |
^ Scaling keys |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
508 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
509 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
510 |
UnitConverter scalings |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
511 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
512 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
513 |
"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
|
514 |
"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
|
515 |
! |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
516 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
517 |
units |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
518 |
"return the set of known units" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
519 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
520 |
|setOfUnits| |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
521 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
522 |
setOfUnits := IdentitySet new. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
523 |
Conversions keysAndValuesDo:[:srcUnit :conversionInfo | |
601 | 524 |
setOfUnits add:srcUnit. |
525 |
conversionInfo keysDo:[:targetUnit | |
|
526 |
setOfUnits add:targetUnit |
|
527 |
] |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
528 |
]. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
529 |
^ setOfUnits |
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 |
" |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
532 |
UnitConverter units |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
533 |
" |
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 |
"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
|
536 |
"Modified: 6.8.1997 / 16:57:47 / cg" |
363 | 537 |
! ! |
538 |
||
539 |
!UnitConverter class methodsFor:'conversions'! |
|
540 |
||
541 |
convert:howMany from:sourceUnit to:destUnit |
|
542 |
"given a value in sourceUnit (symbolic), try to convert it |
|
543 |
to destUnit (symbolic); return nil, if the conversion is |
|
544 |
unknown." |
|
545 |
||
516 | 546 |
|u conversions alias rslt sU dU const val unit |
547 |
i suNumerator suDenominator duNumerator duDenominator uN uD |
|
548 |
sF1 sF2 dF1 dF2 |
|
549 |
s d| |
|
363 | 550 |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
551 |
"/ Transcript showCR:('try ' , sourceUnit , '->' , destUnit). |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
552 |
|
550 | 553 |
Conversions isNil ifTrue:[ |
1031 | 554 |
self initializeConversions |
550 | 555 |
]. |
556 |
||
363 | 557 |
"/ somehow, recursion must end ... |
558 |
sourceUnit == destUnit ifTrue:[ |
|
1031 | 559 |
^ howMany |
363 | 560 |
]. |
561 |
||
369 | 562 |
sourceUnit isSymbol ifFalse:[ |
1031 | 563 |
s := sourceUnit withoutSeparators. |
564 |
sU := s asSymbolIfInterned. |
|
565 |
sU notNil ifTrue:[ |
|
566 |
^ self convert:howMany from:sU to:destUnit |
|
567 |
]. |
|
568 |
(s startsWith:$() ifTrue:[ |
|
569 |
(s endsWith:$)) ifTrue:[ |
|
570 |
s := s copyFrom:2 to:(s size - 1). |
|
571 |
^ self convert:howMany from:s to:destUnit |
|
572 |
] |
|
573 |
]. |
|
369 | 574 |
]. |
575 |
destUnit isSymbol ifFalse:[ |
|
1031 | 576 |
d := destUnit withoutSeparators. |
577 |
dU := d asSymbolIfInterned. |
|
578 |
dU notNil ifTrue:[ |
|
579 |
^ self convert:howMany from:sourceUnit to:dU |
|
580 |
]. |
|
581 |
(d startsWith:$() ifTrue:[ |
|
582 |
(d endsWith:$)) ifTrue:[ |
|
583 |
d := d copyFrom:2 to:(d size - 1). |
|
584 |
^ self convert:howMany from:sourceUnit to:d |
|
585 |
] |
|
586 |
]. |
|
369 | 587 |
]. |
588 |
||
363 | 589 |
"/ first, get rid of scalers ... |
590 |
||
591 |
u := self unscaled:sourceUnit. |
|
592 |
u notNil ifTrue:[ |
|
1031 | 593 |
^ self convert:(howMany*(u value)) from:(u key) to:destUnit |
363 | 594 |
]. |
595 |
||
596 |
u := self unscaled:destUnit. |
|
597 |
u notNil ifTrue:[ |
|
1031 | 598 |
^ self convert:(howMany/(u value)) from:sourceUnit to:(u key) |
363 | 599 |
]. |
600 |
||
601 |
"/ and of aliases ... |
|
602 |
||
603 |
alias := Aliases at:sourceUnit ifAbsent:nil. |
|
604 |
alias notNil ifTrue:[ |
|
1031 | 605 |
^ self convert:howMany from:alias to:destUnit |
363 | 606 |
]. |
607 |
alias := Aliases at:destUnit ifAbsent:nil. |
|
608 |
alias notNil ifTrue:[ |
|
1031 | 609 |
^ self convert:howMany from:sourceUnit to:alias |
363 | 610 |
]. |
611 |
||
516 | 612 |
"/ any constants ? |
613 |
||
614 |
(Constants includesKey:sourceUnit) ifTrue:[ |
|
1031 | 615 |
const := Constants at:sourceUnit. |
616 |
val := const at:1. |
|
617 |
unit := const at:2. |
|
618 |
^ self convert:(howMany*val) from:unit to:destUnit |
|
516 | 619 |
]. |
620 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
621 |
"/ compounds (^ , / or *) are very naively parsed |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
622 |
"/ 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
|
623 |
"/ 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
|
624 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
625 |
|
363 | 626 |
"/ working with squares or cubics ? |
627 |
||
516 | 628 |
((sourceUnit endsWith:'^2') and:[destUnit endsWith:'^2']) ifTrue:[ |
1031 | 629 |
sU := (sourceUnit copyWithoutLast:2) asSymbolIfInterned. |
630 |
dU := (destUnit copyWithoutLast:2) asSymbolIfInterned. |
|
631 |
(sU notNil and:[dU notNil]) ifTrue:[ |
|
632 |
^ (self convert:(howMany sqrt) from:sU to:dU) squared |
|
633 |
]. |
|
634 |
^ self noConversionFrom:sourceUnit to:destUnit. |
|
363 | 635 |
]. |
516 | 636 |
((sourceUnit endsWith:'^3') and:[destUnit endsWith:'^3']) ifTrue:[ |
1031 | 637 |
sU := (sourceUnit copyWithoutLast:2) asSymbolIfInterned. |
638 |
dU := (destUnit copyWithoutLast:2) asSymbolIfInterned. |
|
639 |
(sU notNil and:[dU notNil]) ifTrue:[ |
|
640 |
^ (self convert:(howMany raisedTo:(1/3)) from:sU to:dU) raisedTo:3 |
|
641 |
]. |
|
642 |
^ self noConversionFrom:sourceUnit to:destUnit. |
|
516 | 643 |
]. |
644 |
||
645 |
"/ working with fractions ? |
|
646 |
||
647 |
((sourceUnit includes:$/) and:[destUnit includes:$/]) ifTrue:[ |
|
1031 | 648 |
"/ look for a constant conversion factor |
649 |
i := sourceUnit indexOf:$/. |
|
650 |
suNumerator := sourceUnit copyTo:(i - 1). |
|
651 |
suDenominator := sourceUnit copyFrom:(i + 1). |
|
652 |
i := destUnit indexOf:$/. |
|
653 |
duNumerator := destUnit copyTo:(i - 1). |
|
654 |
duDenominator := destUnit copyFrom:(i + 1). |
|
516 | 655 |
|
1031 | 656 |
uN := self convert:howMany from:suNumerator to:duNumerator. |
657 |
uN notNil ifTrue:[ |
|
658 |
uD := self convert:1 from:suDenominator to:duDenominator. |
|
659 |
uD notNil ifTrue:[ |
|
660 |
^ uN / uD |
|
661 |
] |
|
662 |
]. |
|
516 | 663 |
]. |
664 |
||
665 |
"/ working with products ? |
|
666 |
||
667 |
((sourceUnit includes:$*) and:[destUnit includes:$*]) ifTrue:[ |
|
1031 | 668 |
i := sourceUnit indexOf:$*. |
669 |
sF1 := sourceUnit copyTo:(i - 1). |
|
670 |
sF2 := sourceUnit copyFrom:(i + 1). |
|
671 |
i := destUnit indexOf:$*. |
|
672 |
dF1 := destUnit copyTo:(i - 1). |
|
673 |
dF2 := destUnit copyFrom:(i + 1). |
|
516 | 674 |
|
1031 | 675 |
u := self convert:howMany from:sF1 to:dF1. |
676 |
u notNil ifTrue:[ |
|
677 |
u := self convert:u from:sF2 to:dF2. |
|
678 |
u notNil ifTrue:[ |
|
679 |
^ u |
|
680 |
] |
|
681 |
]. |
|
363 | 682 |
]. |
683 |
||
684 |
"/ the real work comes here ... |
|
685 |
||
686 |
"/ is there a direct conversion in the dataBase ? |
|
687 |
||
688 |
rslt := self convertDirect:howMany from:sourceUnit to:destUnit. |
|
689 |
rslt notNil ifTrue:[^ rslt]. |
|
690 |
||
691 |
"/ try inverse conversion ... |
|
692 |
||
693 |
rslt := self convertDirect:1 from:destUnit to:sourceUnit. |
|
694 |
rslt notNil ifTrue:[ ^ howMany / rslt]. |
|
695 |
||
516 | 696 |
"/ here's the deep recursion ... |
363 | 697 |
|
698 |
||
699 |
"/ try indirect conversion from source |
|
700 |
||
701 |
conversions := Conversions at:sourceUnit ifAbsent:nil. |
|
702 |
conversions notNil ifTrue:[ |
|
1031 | 703 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 | |
704 |
|factor2| |
|
363 | 705 |
|
1031 | 706 |
factor2 := self convert:factor1 from:intermediateUnit to:destUnit. |
707 |
factor2 notNil ifTrue:[^ factor2 * howMany]. |
|
708 |
]. |
|
363 | 709 |
]. |
710 |
||
711 |
"/ try indirect conversion from dest |
|
712 |
||
713 |
conversions := Conversions at:destUnit ifAbsent:nil. |
|
714 |
conversions notNil ifTrue:[ |
|
1031 | 715 |
conversions keysAndValuesDo:[:intermediateUnit :factor1 | |
716 |
|factor2| |
|
363 | 717 |
|
1031 | 718 |
factor2 := self convert:(factor1) from:intermediateUnit to:sourceUnit. |
719 |
factor2 notNil ifTrue:[^ howMany / factor2]. |
|
720 |
]. |
|
363 | 721 |
]. |
722 |
||
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
723 |
"/ 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
|
724 |
|
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
725 |
(sourceUnit includes:$*) ifTrue:[ |
1031 | 726 |
i := sourceUnit indexOf:$*. |
727 |
sF1 := sourceUnit copyTo:(i - 1). |
|
728 |
sF2 := sourceUnit copyFrom:(i + 1). |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
729 |
|
1031 | 730 |
"/ see what we have ... |
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
731 |
|
1031 | 732 |
self units do:[:aUnit | |
733 |
|pref iUnit factor2 rslt| |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
734 |
|
1031 | 735 |
pref := sF1 , '*'. |
736 |
(aUnit startsWith:pref) ifTrue:[ |
|
737 |
"/ ok; want a/b -> x |
|
738 |
"/ found a/c -> any |
|
739 |
"/ what about c->b ? |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
740 |
|
1031 | 741 |
iUnit := aUnit copyFrom:pref size + 1. |
742 |
factor2 := self convert:1 from:sF2 to:iUnit. |
|
743 |
factor2 notNil ifTrue:[ |
|
744 |
"/ good ... |
|
745 |
rslt := self convert:(factor2 * howMany) |
|
746 |
from:aUnit to:destUnit. |
|
747 |
rslt notNil ifTrue:[^ rslt]. |
|
748 |
] |
|
749 |
] |
|
750 |
]. |
|
555
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
751 |
]. |
14f0caf53e2f
more examples; added conversion rule (product expansion)
Claus Gittinger <cg@exept.de>
parents:
550
diff
changeset
|
752 |
|
516 | 753 |
^ self noConversionFrom:sourceUnit to:destUnit. |
363 | 754 |
|
755 |
"direct - how many meters are there in two inches: |
|
756 |
UnitConverter convert:2 from:#inch to:#meter |
|
515 | 757 |
UnitConverter convert:37 from:#degrees to:#fahrenheit |
363 | 758 |
|
759 |
reverse - how many inches are there in one meter |
|
760 |
UnitConverter convert:1 from:#meter to:#inch |
|
761 |
||
762 |
with alias: |
|
763 |
UnitConverter convert:1 from:#inch to:#m |
|
764 |
UnitConverter convert:1 from:#inch to:#mm |
|
765 |
UnitConverter convert:1 from:#inch to:#millimeter |
|
766 |
UnitConverter convert:1 from:#inch to:#nanometer |
|
767 |
UnitConverter convert:1 from:#mm to:#km |
|
768 |
UnitConverter convert:1 from:#km to:#foot |
|
769 |
||
770 |
indirect: |
|
771 |
UnitConverter convert:1 from:#mm to:#twip |
|
772 |
UnitConverter convert:1 from:#inch to:#twip |
|
369 | 773 |
UnitConverter convert:1 from:'letterH' to:#point |
774 |
UnitConverter convert:1 from:'letterlH' to:#point |
|
364 | 775 |
|
776 |
UnitConverter convert:5 from:#barrel to:#liter |
|
777 |
UnitConverter convert:10 from:#kilogram to:#carat |
|
778 |
||
779 |
UnitConverter convert:1 from:#liter to:#floz |
|
363 | 780 |
" |
781 |
||
782 |
"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
|
783 |
"Modified: 6.8.1997 / 18:10:22 / cg" |
363 | 784 |
! |
785 |
||
1292 | 786 |
fileSizeFromString:fileSizeString |
787 |
"given a fileSize string (i.e. a number followed by optional Kb, Mb or Gb as required. |
|
788 |
Provided here since this is so common..." |
|
789 |
||
790 |
|numericValue s scaleChar scale| |
|
791 |
||
792 |
fileSizeString isEmpty ifTrue:[ |
|
793 |
^ 0 |
|
794 |
]. |
|
795 |
||
796 |
scale := 1. |
|
797 |
s := fileSizeString readStream. |
|
798 |
numericValue := Number readFrom:s onError:[0]. |
|
799 |
s skipSeparators. |
|
800 |
s atEnd ifFalse:[ |
|
801 |
scaleChar := s next asLowercase. |
|
802 |
scaleChar = $k ifTrue:[ |
|
803 |
scale := 1024 |
|
804 |
] ifFalse:[ |
|
805 |
scaleChar = $m ifTrue:[ |
|
806 |
scale := 1024 * 1024 |
|
807 |
] ifFalse:[ |
|
808 |
scaleChar = $g ifTrue:[ |
|
809 |
scale := 1024 * 1024 * 1024 |
|
810 |
] ifFalse:[ |
|
811 |
scaleChar = $t ifTrue:[ |
|
812 |
scale := 1024 * 1024 * 1024 * 1024 |
|
813 |
] |
|
814 |
] |
|
815 |
] |
|
816 |
]. |
|
817 |
]. |
|
818 |
^ (numericValue*scale) asInteger |
|
819 |
||
820 |
" |
|
821 |
self fileSizeFromString:'1234' |
|
1295 | 822 |
self fileSizeFromString:'10 k' |
823 |
self fileSizeFromString:'0.6 k' |
|
824 |
self fileSizeFromString:'1.8 M' |
|
825 |
self fileSizeFromString:'0.5 G' |
|
826 |
self fileSizeFromString:'0.5 T' |
|
1292 | 827 |
" |
828 |
! |
|
829 |
||
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
830 |
fileSizeStringFor:nBytes |
1293 | 831 |
"return a useful string for a size-in-bytes; |
1294 | 832 |
Scales by K (*1024), M (*1024*1024) or G (*1024*1024*1024) or even T |
833 |
as required and useful. |
|
1143 | 834 |
Provided here since this is so common..." |
835 |
||
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
836 |
|unitString n| |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
837 |
|
1293 | 838 |
nBytes < (1000 * 1024 * 1024 * 1024) ifTrue:[ |
839 |
nBytes < (1000 * 1024 * 1024) ifTrue:[ |
|
840 |
nBytes < (1000 * 1024) ifTrue:[ |
|
1457 | 841 |
nBytes < 1000 ifTrue:[ |
1293 | 842 |
n := nBytes. |
843 |
unitString := ' '. |
|
844 |
] ifFalse:[ |
|
845 |
n := (nBytes / 1024) asFixedPoint:1. |
|
846 |
unitString := ' K' |
|
847 |
] |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
848 |
] ifFalse:[ |
1293 | 849 |
n := (nBytes / (1024 * 1024)) asFixedPoint:1. |
850 |
unitString := ' M' |
|
851 |
]. |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
852 |
] ifFalse:[ |
1293 | 853 |
n := (nBytes / 1024 / (1024 * 1024)) asFixedPoint:1. |
854 |
unitString := ' G' |
|
855 |
] |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
856 |
] ifFalse:[ |
1293 | 857 |
n := (nBytes / (1024 * 1024) / (1024 * 1024)) asFixedPoint:1. |
858 |
unitString := ' T' |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
859 |
]. |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
860 |
|
1346 | 861 |
"/ n truncated = n ifTrue:[ |
862 |
"/ n := n truncated |
|
863 |
"/ ]. |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
864 |
^ n printString , unitString. |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
865 |
|
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
866 |
" |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
867 |
self fileSizeStringFor:10 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
868 |
self fileSizeStringFor:100 |
1298
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
869 |
self fileSizeStringFor:1000 |
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
870 |
self fileSizeStringFor:8192 |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
871 |
self fileSizeStringFor:10000 |
1298
7e831af30f44
return integer if possible for fileSize conversions
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
872 |
self fileSizeStringFor:100000 |
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
873 |
self fileSizeStringFor:1000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
874 |
self fileSizeStringFor:10000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
875 |
self fileSizeStringFor:100000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
876 |
self fileSizeStringFor:1000000000 |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
877 |
self fileSizeStringFor:10000000000 |
1293 | 878 |
self fileSizeStringFor:100000000000 |
879 |
self fileSizeStringFor:1000000000000 |
|
880 |
self fileSizeStringFor:10000000000000 |
|
1132
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
881 |
" |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
882 |
! |
2f0305cb691b
fileSizeString added (Kb/Mb/Gb)
Claus Gittinger <cg@exept.de>
parents:
1031
diff
changeset
|
883 |
|
363 | 884 |
inchToMillimeter:inches |
364 | 885 |
"convert inches to mm. Made this an extra method, since its so common." |
363 | 886 |
|
887 |
^ inches * 25.4 |
|
888 |
||
889 |
"Created: 31.5.1996 / 13:37:31 / cg" |
|
364 | 890 |
"Modified: 31.5.1996 / 18:08:14 / cg" |
363 | 891 |
! |
892 |
||
893 |
millimeterToInch:mm |
|
364 | 894 |
"convert mm to inches. Made this an extra method, since its so common." |
363 | 895 |
|
896 |
^ mm / 25.4 |
|
897 |
||
898 |
"Created: 31.5.1996 / 13:37:48 / cg" |
|
364 | 899 |
"Modified: 31.5.1996 / 18:08:20 / cg" |
363 | 900 |
! ! |
901 |
||
516 | 902 |
!UnitConverter class methodsFor:'missing conversion'! |
903 |
||
904 |
noConversionFrom:sourceUnit to:destUnit |
|
905 |
Transcript showCR:'no conversion ' , sourceUnit printString , ' -> ' , destUnit printString. |
|
906 |
^ nil |
|
907 |
||
908 |
"Created: 29.3.1997 / 17:56:03 / cg" |
|
909 |
! ! |
|
910 |
||
363 | 911 |
!UnitConverter class methodsFor:'private'! |
912 |
||
913 |
addConversion:factor from:sourceMetric to:destMetric |
|
914 |
"add a conversion" |
|
915 |
||
916 |
|conversion| |
|
917 |
||
918 |
conversion := Conversions at:sourceMetric ifAbsent:nil. |
|
919 |
conversion isNil ifTrue:[ |
|
601 | 920 |
conversion := IdentityDictionary new. |
921 |
Conversions at:sourceMetric put:conversion |
|
363 | 922 |
]. |
923 |
conversion at:destMetric put:factor. |
|
924 |
^ conversion |
|
925 |
||
926 |
"Created: 31.5.1996 / 13:51:25 / cg" |
|
515 | 927 |
"Modified: 29.3.1997 / 17:19:31 / cg" |
363 | 928 |
! |
929 |
||
930 |
convertDirect:howMany from:sourceMetric to:destMetric |
|
931 |
"given a value in sourceMetric (symbolic), try to convert it |
|
932 |
to destMetric (symbolic); |
|
933 |
Only direct conversions are tried; return nil, if the conversion is unknown." |
|
934 |
||
935 |
|conversion factor| |
|
936 |
||
550 | 937 |
Conversions isNil ifTrue:[ |
601 | 938 |
self initializeConversions |
550 | 939 |
]. |
940 |
||
363 | 941 |
conversion := Conversions at:sourceMetric ifAbsent:nil. |
942 |
conversion isNil ifTrue:[^ nil]. |
|
943 |
||
944 |
factor := conversion at:destMetric ifAbsent:nil. |
|
945 |
factor isNil ifTrue:[^ nil]. |
|
946 |
||
364 | 947 |
factor isNumber ifTrue:[ |
601 | 948 |
^ howMany * factor |
364 | 949 |
]. |
515 | 950 |
^ factor value:howMany |
363 | 951 |
|
952 |
" |
|
953 |
UnitConverter convertDirect:1 from:#inch to:#meter |
|
954 |
UnitConverter convertDirect:1 from:#inch to:#millimeter |
|
515 | 955 |
UnitConverter convertDirect:1 from:#degrees to:#fahrenheit |
363 | 956 |
" |
957 |
||
958 |
"Created: 31.5.1996 / 13:54:33 / cg" |
|
550 | 959 |
"Modified: 22.7.1997 / 13:57:52 / cg" |
363 | 960 |
! |
961 |
||
962 |
unscaled:what |
|
963 |
"given a unit, return the base and a factor as assoc, |
|
964 |
or nil if not found" |
|
965 |
||
550 | 966 |
Conversions isNil ifTrue:[ |
601 | 967 |
self initializeConversions |
550 | 968 |
]. |
969 |
||
363 | 970 |
Scaling keysAndValuesDo:[:name :factor | |
601 | 971 |
|rest| |
363 | 972 |
|
601 | 973 |
(what startsWith:name) ifTrue:[ |
974 |
rest := what copyFrom:(name size+1). |
|
975 |
rest := rest asSymbolIfInterned. |
|
976 |
rest notNil ifTrue:[ |
|
977 |
^ rest -> factor. |
|
978 |
] |
|
979 |
]. |
|
363 | 980 |
]. |
981 |
^ nil |
|
982 |
||
983 |
" |
|
984 |
UnitConverter unscaled:#millimeter |
|
985 |
UnitConverter unscaled:#nanometer |
|
986 |
UnitConverter unscaled:#kilometer |
|
987 |
UnitConverter unscaled:#fuzzymeter |
|
988 |
" |
|
989 |
||
550 | 990 |
"Modified: 22.7.1997 / 13:57:46 / cg" |
363 | 991 |
! ! |
992 |
||
993 |
!UnitConverter class methodsFor:'documentation'! |
|
994 |
||
995 |
version |
|
2320 | 996 |
^ '$Header: /cvs/stx/stx/libbasic2/UnitConverter.st,v 1.36 2009-10-27 12:19:01 cg Exp $' |
997 |
! |
|
998 |
||
999 |
version_CVS |
|
1000 |
^ '$Header: /cvs/stx/stx/libbasic2/UnitConverter.st,v 1.36 2009-10-27 12:19:01 cg Exp $' |
|
363 | 1001 |
! ! |