|
|
Class: UnitConverter
Object
|
+--UnitConverter
- Package:
- stx:libbasic2
- Category:
- Magnitude-General
- Version:
- rev:
1.38
date: 2010/04/18 14:11:29
- user: cg
- file: UnitConverter.st directory: libbasic2
- module: stx stc-classLibrary: libbasic2
- Author:
- Claus Gittinger
In order to collect various unit conversions into one central
manageable (and especially: browsable) place, all previously
spread knowledge about metric (and other) conversions has been
brought into this class.
This class is purely functional, abstract and has no instances;
all queries are via class protocol.
Choosing the Magnitudes category as its home is arbitrary.
The supported units are setup in the classes initialize method;
supported are:
meter - inch - feet - foot - yard
meter2 - acre - 'german-ar' 'german-hektar' are
liter - gallon - barrel - floz
celsius - fahrenheit
and many others.
The converter does a recursive search for a conversion;
thus, if there is a conversion from #inch to #millimeter and another
from #millimeter to #kilometer, conversion from #inch to #kilometer
is found automatically.
No Warranty:
The numbers and conversion factors were obtained from the Unix
units command. Please check before using it - there might be
typos or wrong conversions in the setup code.
Notice:
This class is *much* older than the younger and more object oriented
Measurements package. Other than solving similar problems, these two packages
are not related.
see also:
examples
/usr/share/lib/unittab (if present on your system)
Measurement Unit
accessing
-
scalings
-
return the set of known scaling prefixes
-
units
-
return the set of known units
conversions
-
convert: howMany from: sourceUnit to: destUnit
-
given a value in sourceUnit (symbolic), try to convert it
to destUnit (symbolic); return nil, if the conversion is
unknown.
-
fileSizeFromString: fileSizeString
-
given a fileSize string (i.e. a number followed by optional Kb, Mb or Gb as required.
Provided here since this is so common...
-
fileSizeStringFor: nBytes
-
return a useful string for a size-in-bytes;
Scales by K (*1024), M (*1024*1024) or G (*1024*1024*1024) or even T
as required and useful.
Provided here since this is so common...
-
inchToMillimeter: inches
-
convert inches to mm. Made this an extra method, since its so common.
-
millimeterToInch: mm
-
convert mm to inches. Made this an extra method, since its so common.
initialization
-
initializeConversions
-
initialize common conversions
-
initializePrintValues
-
inch to (roughly) a typesetter point
-
initializeScaleFactors
-
missing conversion
-
noConversionFrom: sourceUnit to: destUnit
-
private
-
addConversion: factor from: sourceMetric to: destMetric
-
add a conversion
-
convertDirect: howMany from: sourceMetric to: destMetric
-
given a value in sourceMetric (symbolic), try to convert it
to destMetric (symbolic);
Only direct conversions are tried; return nil, if the conversion is unknown.
-
unscaled: what
-
given a unit, return the base and a factor as assoc,
or nil if not found
ever wanted to know, how many floz's are there in a european
Coce bottle ?
Transcript showCR:
(UnitConverter convert:1 from:#liter to:#floz)
|
Transcript showCR:
(UnitConverter convert:43.5 from:#oz to:#gram)
|
or, how many square-meters an acre is:
Transcript showCR:
(UnitConverter convert:1 from:#acre to:#'meter^2')
|
or europeans might want to know, what those
fahrenheit numbers mean in an US weather report ;-):
Transcript showCR:(
(UnitConverter convert:80 from:#fahrenheit to:#celsius)
asFixedPoint:1)
|
how fast do I drive ? :-)
Transcript showCR:(
UnitConverter convert:200 from:#'km/hr' to:#'mile/hr')
|
how fast does Chris drive ? :-)
Transcript showCR:(
UnitConverter convert:65 from:#'mile/hr' to:#'km/hr')
|
calories or joule ?
Transcript showCR:(
UnitConverter convert:0.18 from:#'kilocalorie' to:#'kilojoule')
|
Transcript showCR:(
UnitConverter convert:2000 from:#'kilocalorie' to:#'kilojoule')
|
distances:
Transcript showCR:(
UnitConverter convert:1 from:#'lightsecond' to:#'kilometer')
|
thats the same:
Transcript showCR:(
UnitConverter convert:1 from:#'lightspeed*s' to:#'kilometer')
|
a days travel ...
Transcript showCR:(
UnitConverter convert:1 from:#'lightspeed*dy' to:#'kilometer')
|
a year travel ...
Transcript showCR:(
UnitConverter convert:1 from:#'lightyear' to:#'kilometer')
|
wonna race on the autobahn ?
Transcript showCR:(
UnitConverter convert:200 from:#'km/h' to:#'mph')
|
real estate buyers might want to know, how many acres
a german ar is:
Transcript showCR:
(UnitConverter convert:1 from:#'german-ar' to:#acre)
|
- or how many square-feet are there in your living room:
Transcript showCR:
(UnitConverter convert:100 from:#'meter^2' to:#'foot^2')
|
- or how about:
Transcript showCR:
(UnitConverter convert:3600 from:#'ft*ft' to:#'m*,')
|
how many tea spoons are there in a cubic meter ?
(roughly, since a teaspoon is not a standard unit)
Transcript showCR:
(UnitConverter convert:1 from:#'meter^3' to:#teaspoon)
|
how wide is a US page in inches:
Transcript showCR:
(UnitConverter convert:1 from:#'letterW' to:#inch)
|
- in millimeter:
Transcript showCR:
(UnitConverter convert:1 from:#'letterW' to:#mm)
|
the height of a US page in inches:
Transcript showCR:
(UnitConverter convert:1 from:#'letterH' to:#inch)
|
- in millimeter:
Transcript showCR:
(UnitConverter convert:1 from:#'letterH' to:#mm)
|
the same for european A4 standard page:
Transcript showCR:
(UnitConverter convert:1 from:#'a4H' to:#mm)
|
Transcript showCR:
(UnitConverter convert:1 from:#'a4H' to:#inch)
|
the mass of a proton:
Transcript showCR:
(UnitConverter convert:1 from:#'proton' to:#eV)
|
the energy of a single proton in the LHC (as of 2010)
Transcript showCR:
(UnitConverter convert:7 from:#'TeV' to:#joule)
|
|