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