eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'UnitConverter':

Home

everywhere
www.exept.de
for:
[back]

Class: UnitConverter


Inheritance:

   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

Description:


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


Class protocol:

accessing
o  scalings
return the set of known scaling prefixes

o  units
return the set of known units

conversions
o  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.

o  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...

o  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...

o  inchToMillimeter: inches
convert inches to mm. Made this an extra method, since its so common.

o  millimeterToInch: mm
convert mm to inches. Made this an extra method, since its so common.

initialization
o  initializeConversions
initialize common conversions

o  initializePrintValues
inch to (roughly) a typesetter point

o  initializeScaleFactors

missing conversion
o  noConversionFrom: sourceUnit to: destUnit

private
o  addConversion: factor from: sourceMetric to: destMetric
add a conversion

o  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.

o  unscaled: what
given a unit, return the base and a factor as assoc,
or nil if not found


Examples:


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)   


ST/X 6.1.1; WebServer 1.620 at exept:8081; Tue, 22 May 2012 22:57:30 GMT