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