[system] / trunk / pg / macros / contextCurrency.pl Repository:
ViewVC logotype

Diff of /trunk/pg/macros/contextCurrency.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 5550 Revision 5551
1=head1 Context("Currency");
2
3#####################################################################
4
5This file implements a context in which students can enter currency
6values that include a currency symbol and commas every three digits.
7You can specify what the currency symbol is, as well as what gets
8used for commas and decimals.
9
10To use the context, put
11
12 loadMacros("contextCurrency.pl");
13
14at the top of your problem file, and then issue the
15
16 Context("Currency");
17
18command to select the context. You can set the currency symbol
19and the comma or decimal values as in the following examples
20
21 Context()->currency->set(symbol=>'#');
22 Context()->currency->set(symbol=>'euro'); # accepts '12 euro'
23 Context()->currency->set(comma=>'.',decimal=>','); # accepts '10.000,00'
24
25You can add additional symbols (in case you want to allow
26more than one way to write the currency). For example:
27
28 Context("Currency")->currency->addSymbol("dollars","dollar");
29
30would accept '$12,345.67' or '12.50 dollars' or '1 dollar' as
31acceptable values. Note that if the symbol cantains any
32alphabetic characters, it is expected to come at the end of the
33number (as in the examples above) and if the symbol has only
34non-alphabetic characters, it comes before it. You can change
35this as in these examples:
36
37 Context()->currency->setSymbol(euro=>{associativity=>"left"});
38 Context()->currency->setSymbol('#'=>{associativity=>"right"});
39
40You can remove a symbol as follows:
41
42 Context()->currency->removeSymbol('dollar');
43
44To create a currency value, use
45
46 $m = Currency(10.99);
47
48or
49
50 $m1 = Compute('$10.99');
51 $m2 = Compute('$10,000.00');
52
53and so on. Be careful, however, that you do not put dollar signs
54inside double quotes, as this refers to variable substitution.
55For example,
56
57 $m = Compute("$10.99");
58
59will most likely set $m to the Real value .99 rather than the
60monitary value of $10.99, since perl thinks $10 is the name of
61a variable, and will substitute that into the string before
62processing it. Since that variable is most likely empty, the
63result will be the same as $m = Compute(".99");
64
65You can use monitary values within computations, as in
66
67 $m1 = Compute('$10.00');
68 $m2 = 3*$m1; $m3 = $m2 + .5;
69 $m4 = Compute('$10.00 + $2.59');
70
71so that $m2 will be $30.00, $m3 will be $30.50, and $m4 will
72be $12.59. Students can perform computations within their
73answers unless you disable the operators and functions as well.
74
75The tolerance for this context is set initially to .005 and the
76tolType to 'absolute' so that monitary values will have to match
77to the nearest penny. You can change that on a global basis
78using
79
80 Context()->flags->set(tolerance=>.0001,tolType=>"relative");
81
82for example. You can also change the tolerance on an individual
83currency value as follows:
84
85 $m = Compute('$1,250,000.00')->with(tolerance=>.0001,tolType=>'relative');
86
87By default, the answer checker for Currency values requires
88the student to enter the currency symbol, not just a real number.
89You can relax that condition by including the promoteReals=>1
90option to the cmp() method of the Currency value. For example,
91
92 ANS(Compute('$150')->cmp(promoteReals=>1));
93
94would allow the student to enter just 150 rather than $150.
95
96By default, the students may omit the commas, but you can
97force them to supply the commas using forceCommas=>1 in
98your cmp() call.
99
100 ANS(Compute('$10,000.00')->cmp(forceCommas=>1));
101
102By default, students need not enter decimal digits, so could use
103$100 or $1,000. as valid entries. You can require that the cents
104be provided using the forceDecimals=>1 flag.
105
106 ANS(Compute('$10.95')->cmp(forceDecimals=>1));
107
108By default, if the monitary value includes decimals digits, it
109must have exactly two. You can weaken this requirement to all
110any number of decimals by using noExtraDecimals=>0.
111
112 ANS(Compute('$10.23372')->cmp(noExtraDecimals=>0);
113
114If forceDecimals is set to 1 at the same time, then they must
115have 2 or more decimals, otherwise any number is OK.
116
117=cut
118
1loadMacros("MathObjects.pl"); 119loadMacros("MathObjects.pl");
2loadMacros("problemPreserveAnswers.pl"); # needed to preserve $ in answers 120loadMacros("problemPreserveAnswers.pl"); # needed to preserve $ in answers
3 121
4sub _contextCurrency_init {Currency::Init()} 122sub _contextCurrency_init {Currency::Init()}
5
6=head1 Context("Currency");
7
8 ######################################################################
9 #
10 # This file implements a context in which students can enter currency
11 # values that include a currency symbol and commas every three digits.
12 # You can specify what the currency symbol is, as well as what gets
13 # used for commas and decimals.
14 #
15 # To use the context, put
16 #
17 # loadMacros("contextCurrency.pl");
18 #
19 # at the top of your problem file, and then issue the
20 #
21 # Context("Currency");
22 #
23 # command to select the context. You can set the currency symbol
24 # and the comma or decimal values as in the following examples
25 #
26 # Context()->currency->set(symbol=>'#');
27 # Context()->currency->set(symbol=>'euro'); # accepts '12 euro'
28 # Context()->currency->set(comma=>'.',decimal=>','); # accepts '10.000,00'
29 #
30 # You can add additional symbols (in case you want to allow
31 # more than one way to write the currency). For example:
32 #
33 # Context("Currency")->currency->addSymbol("dollars","dollar");
34 #
35 # would accept '$12,345.67' or '12.50 dollars' or '1 dollar' as
36 # acceptable values. Note that if the symbol cantains any
37 # alphabetic characters, it is expected to come at the end of the
38 # number (as in the examples above) and if the symbol has only
39 # non-alphabetic characters, it comes before it. You can change
40 # this as in these examples:
41 #
42 # Context()->currency->setSymbol(euro=>{associativity=>"left"});
43 # Context()->currency->setSymbol('#'=>{associativity=>"right"});
44 #
45 # You can remove a symbol as follows:
46 #
47 # Context()->currency->removeSymbol('dollar');
48 #
49 # To create a currency value, use
50 #
51 # $m = Currency(10.99);
52 #
53 # or
54 #
55 # $m1 = Compute('$10.99');
56 # $m2 = Compute('$10,000.00');
57 #
58 # and so on. Be careful, however, that you do not put dollar signs
59 # inside double quotes, as this refers to variable substitution.
60 # For example,
61 #
62 # $m = Compute("$10.99");
63 #
64 # will most likely set $m to the Real value .99 rather than the
65 # monitary value of $10.99, since perl thinks $10 is the name of
66 # a variable, and will substitute that into the string before
67 # processing it. Since that variable is most likely empty, the
68 # result will be the same as $m = Compute(".99");
69 #
70 # You can use monitary values within computations, as in
71 #
72 # $m1 = Compute('$10.00');
73 # $m2 = 3*$m1; $m3 = $m2 + .5;
74 # $m4 = Compute('$10.00 + $2.59');
75 #
76 # so that $m2 will be $30.00, $m3 will be $30.50, and $m4 will
77 # be $12.59. Students can perform computations within their
78 # answers unless you disable the operators and functions as well.
79 #
80 # The tolerance for this context is set initially to .005 and the
81 # tolType to 'absolute' so that monitary values will have to match
82 # to the nearest penny. You can change that on a global basis
83 # using
84 #
85 # Context()->flags->set(tolerance=>.0001,tolType=>"relative");
86 #
87 # for example. You can also change the tolerance on an individual
88 # currency value as follows:
89 #
90 # $m = Compute('$1,250,000.00')->
91 # with(tolerance=>.0001,tolType=>'relative');
92 #
93 # By default, the answer checker for Currency values requires
94 # the student to enter the currency symbol, not just a real number.
95 # You can relax that condition by including the promoteReals=>1
96 # option to the cmp() method of the Currency value. For example,
97 #
98 # ANS(Compute('$150')->cmp(promoteReals=>1));
99 #
100 # would allow the student to enter just 150 rather than $150.
101 #
102 # By default, the students may omit the commas, but you can
103 # force them to supply the commas using forceCommas=>1 in
104 # your cmp() call.
105 #
106 # ANS(Compute('$10,000.00')->cmp(forceCommas=>1));
107 #
108 # By default, students need not enter decimal digits, so could use
109 # $100 or $1,000. as valid entries. You can require that the cents
110 # be provided using the forceDecimals=>1 flag.
111 #
112 # ANS(Compute('$10.95')->cmp(forceDecimals=>1));
113 #
114 # By default, if the monitary value includes decimals digits, it
115 # must have exactly two. You can weaken this requirement to all
116 # any number of decimals by using noExtraDecimals=>0.
117 #
118 # ANS(Compute('$10.23372')->cmp(noExtraDecimals=>0);
119 #
120 # If forceDecimals is set to 1 at the same time, then they must
121 # have 2 or more decimals, otherwise any number is OK.
122 #
123 ######################################################################
124
125=cut
126 123
127package Currency; 124package Currency;
128 125
129# 126#
130# Initialization creates a Currency context object 127# Initialization creates a Currency context object

Legend:
Removed from v.5550  
changed lines
  Added in v.5551

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9