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

Legend:
Removed from v.5372  
changed lines
  Added in v.5373

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9