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