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