Unit & Quantity examples using com-hfg objects.

The com.hfg.units package contains classes for working with units and quantities of length, volume, time, area, temperature, concentration, amount of substance (mole), electric current, luminous intensity, biological potency, and catalytic activity. Here are some simple examples.

  1. Quantity constructor examples
  2. Plurals
  3. Scaling a length
  4. Scaling a concentration
  5. Length conversion from meters to feet or inches
  6. Length conversion from feet to rods
  7. Area conversion from acres to sq. meters
  8. Temperature conversion from Kelvin to Farenheit
  9. Quantity addition with differing units
  10. Quantity subtraction with differing units
  11. Quantity less than comparison with differing units
  12. Quantity greater than comparison with differing units

Example 1: Quantity constructor examples


    // Some SI examples
    Quantity quantity = new Quantity(5000, "meters");
    Assert.assertEquals("5000 m", quantity.toString());

    quantity = new Quantity(30.2, "microliters");
    Assert.assertEquals("30.2 μL", quantity.toString());

    quantity = new Quantity(30.2, "μL");
    Assert.assertEquals("30.2 μL", quantity.toString());

    quantity = new Quantity("+0.5 L");
    Assert.assertEquals("0.5 L", quantity.toString());

    quantity = new Quantity("10 sec");
    Assert.assertEquals("10 s", quantity.toString());

    quantity = new Quantity("1.660539040E-27 g");
    Assert.assertEquals("1.66053904E-27 g", quantity.toString());
    Assert.assertEquals(1.660539040E-27, quantity.floatValue(), 1E-28);
    Assert.assertEquals("g", quantity.getUnit().toString());

    // Some Imperial system examples
    quantity = new Quantity("6 ft.");
    Assert.assertEquals("6 ft", quantity.toString());

    quantity = new Quantity("6 feet");
    Assert.assertEquals("6 ft", quantity.toString());

    quantity = new Quantity("6 lb 4 oz");
    Assert.assertEquals("6.25 lb", quantity.convertTo(Unit.valueOf("lb")).toString());

    quantity = new Quantity("2 gal 2 qt");
    Assert.assertEquals(10, quantity.intValue().intValue());
    Assert.assertEquals("10 qt", quantity.convertTo(Unit.valueOf("qt")).toString());

    quantity = new Quantity("5 ft 10 in");
    Assert.assertEquals("1.778 m", quantity.convertTo(Unit.valueOf("m")).toString());

    quantity = new Quantity("5' 10''");
    Assert.assertEquals("1.778 m", quantity.convertTo(Unit.valueOf("m")).toString());

Example 2: Plurals


    Quantity quantity = new Quantity(1.0, TimeUnit.day);
    Assert.assertEquals("1 day", quantity.toString());

    // Try with a non-one value
    quantity = new Quantity(0.5, TimeUnit.day);
    Assert.assertEquals("0.5 days", quantity.toString());

Example 3: Scaling a length


    Quantity quantity = new Quantity(5000, LengthUnit.meter);

    quantity.scale(QuantityType.LENGTH, SI_ScalingFactor.kilo);
    Assert.assertEquals("5 km", quantity.toString());

    quantity.scale(QuantityType.LENGTH, SI_ScalingFactor.mega);
    Assert.assertEquals("0.005 Mm", quantity.toString());

    quantity.scale(QuantityType.LENGTH, SI_ScalingFactor.centi);
    Assert.assertEquals("500000 cm", quantity.toString());

Example 4: Scaling a concentration


    Quantity quantity = new Quantity(1.2, "mg/mL");

    Assert.assertEquals("1.2 mg/mL", quantity.toString());

    quantity.scale(QuantityType.VOLUME, SI_ScalingFactor.one);
    Assert.assertEquals("1200 mg/L", quantity.toString());

    quantity.scale(QuantityType.MASS, SI_ScalingFactor.one);
    Assert.assertEquals("1.2 g/L", quantity.toString());
    

Example 5: Length conversion from meters to feet or inches


    Quantity quantity = new Quantity(10, LengthUnit.meter);

    Quantity quantityFeet = quantity.convertTo(LengthUnit.foot);
    Assert.assertEquals("32.81 ft", quantityFeet.toString("%.2f"));

    Quantity quantityInches = quantity.convertTo(LengthUnit.inch);
    Assert.assertEquals("393.70 in", quantityInches.toString("%.2f"));

Example 6: Length conversion from feet to rods


    Quantity quantityFeet = new Quantity(10, LengthUnit.foot);

    Quantity quantityRods = quantityFeet.convertTo(LengthUnit.rod);
    Assert.assertEquals("0.61 rods", quantityRods.toString("%.2f"));

Example 7: Area conversion from acres to sq. meters


    Quantity quantityAcres = new Quantity(10, AreaUnit.acre);

    Quantity quantitySqMeters = quantityAcres.convertTo(AreaUnit.sq_meter);
    Assert.assertEquals("40468.564224 m²", quantitySqMeters.toString());

Example 8: Temperature conversion from Kelvin to Farenheit


    Quantity quantityKelvin = new Quantity(288.71, TemperatureUnit.Kelvin);

    Quantity quantityFarenheight = quantityKelvin.convertTo(TemperatureUnit.Farenheit);
    Assert.assertEquals("60.0 °F", quantityFarenheight.toString("%.1f"));

Example 9: Quantity addition with differing units


    Quantity testQuantity = new Quantity(5, Unit.valueOf("kg"));
    testQuantity = testQuantity.add(new Quantity(2, Unit.valueOf("g")));

    Assert.assertEquals("5.002 kg", testQuantity.toString("%.3f"));

Example 10: Quantity subtraction with differing units


    Quantity testQuantity = new Quantity(5, Unit.valueOf("kg"));
    testQuantity = testQuantity.subtract(new Quantity(2, Unit.valueOf("g")));

    Assert.assertEquals("4.998 kg", testQuantity.toString("%.3f"));

Example 11: Quantity less than comparison with differing units


    Quantity testQuantity = new Quantity(5, Unit.valueOf("kg"));

    Assert.assertFalse(testQuantity.lessThan(new Quantity(5, Unit.valueOf("kg"))));
    Assert.assertFalse(testQuantity.lessThan(new Quantity(1, Unit.valueOf("g"))));
    Assert.assertTrue(testQuantity.lessThan(new Quantity(10000, Unit.valueOf("g"))));
    Assert.assertFalse(testQuantity.lessThan(new Quantity(10, Unit.valueOf("lb"))));
    Assert.assertTrue(testQuantity.lessThan(new Quantity(11.3, Unit.valueOf("lb"))));

Example 12: Quantity greater than comparison with differing units


    Quantity testQuantity = new Quantity(5, Unit.valueOf("kg"));

    Assert.assertFalse(testQuantity.greaterThan(new Quantity(5, Unit.valueOf("kg"))));
    Assert.assertTrue(testQuantity.greaterThan(new Quantity(1, Unit.valueOf("g"))));
    Assert.assertFalse(testQuantity.greaterThan(new Quantity(10000, Unit.valueOf("g"))));
    Assert.assertTrue(testQuantity.greaterThan(new Quantity(10, Unit.valueOf("lb"))));
    Assert.assertFalse(testQuantity.greaterThan(new Quantity(11.3, Unit.valueOf("lb"))));
    

Return to Main Page