My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Examples  
JUGC examples
Featured
Updated Feb 4, 2010 by juri.pak...@gmail.com

Examples of JUGC usage

Example unit definitions:

<?xml version="1.0" encoding="UTF-8"?>
<units>
	<unit>
		<id>gram</id>
		<alias>g</alias>
	</unit>

	<unit>
		<id>kilogram</id>
		<alias>kg</alias>
		<derive>gram</derive>
		<from>1000 /</from>
	</unit>
</units>

Doing an unit conversion in Java:

Map<String, Unit> units = GeneratedUnits.createUnitsMap();
cf = new ConverterFactory(units, GeneratedUnits.getTranslations(units));
Converter pipe = cf.getConverter("gram", "kilogram");
pipe.convert(1000); // returns 1.0

Doing an unit conversion in Python:

um = create_units_map()
cf = ConverterFactory(um, get_translations(um))
c = cf.get_converter("g", "kg")
c.convert(1000) # returns 1.0
Comment by michael.bedward@gmail.com, Nov 5, 2008

Hi Juri - many thanks for jugc.

One of the things I like is the generation of implied conversions. Why not illustrate this in the example by adding another unit...

<unit>
    <id>tonne</id>
    <derive>kilogram</derive>
    <from>1000 /</from>
</unit>

Then the example of unit conversion could highlight that you get the implied gram to tonne conversion...

Converter c = cf.getConverter("gram", "tonne");
c.convert(1000000); // returns 1.0

cheers Michael


Sign in to add a comment
Powered by Google Project Hosting