Jump to: Site Navigation

NAME - Implements a number with units.


This is a Parser class that implements a number with units. It is a temporary version until the Parser can handle it directly.

Use NumberWithUnits("num units") or NumberWithUnits(formula,"units") to generate a NumberWithUnits object, and then call its cmp method to get an answer checker for your number with units.

Usage examples:

        ANS(NumberWithUnits("3 ft")->cmp);
        ANS(NumberWithUnits("$a*$b ft")->cmp);

We now call on the Legacy version, which is used by num_cmp to handle numbers with units.

New units can be added at run time by using the newUnit option

       $a = NumberWithUnits("3 apples",{newUnit=>'apples'});

A new unit can either be a string, in which case the string is added as a new unit with no relation to other units, or as a hashreference

       $newUnit = {name => 'bear',
                   conversion => {factor =>3, m=>1}};
       $a = NumberWithUnits("3 bear", {newUnit=>$newUnit});

You can also define your own conversion hash. In the above example one bear is three meters. (See for examples).

Finally, the newUnit option can also be an array ref containing any number of new units to add. A common reason for doing this would be to add the plural version of the unit as an equilvalent unit. E.G.

      $newUnits = ['apple',{name=>'apples',conversion=>{factor=>1,apple=>1}}];
      $a = NumberWithUnits("3 apples",{newUnit=>$newUnits});

In this case both 3 apple and 3 apples would be considered correct.

Note: English pluralization is suprisingly hard, so WeBWorK will make no attempt to display a grammerically correct result.

Site Navigation