From WeBWorK

Jump to: navigation


Context("AlternateDecimal") - Provides a context that allows the entry of decimal numbers using a comma for the decimal indicator rather than a dot (e.g., 3,14159 rather than 3.14159).


This macro file defines contexts in which decimal numbers can be entered using a comma rather than a period as the decimal separator. Both forms are always recognized, but you can determine whether one or the other form produces an error message when used. You can also force the display of numbers to use one or the other form.


To use this file, first load it into your problem, then select the context that you wish to use. There are three pre-defined contexts, AlternateDecimal, AlternateDecimal-Only, and AlternateDecimal-Warning. The first allows both the standard and alternate forms to be used, the second allows only the alternate form, and the third allows only the standard form, but recognizes the alternate form and gives an error message when it is used.

        $r1 = Compute("3.14159");
        $r2 = Compute("3,14159");    # equivalent to $r1;
        $r1 = Compute("3.14159");
        $r2 = Compute("3,14159");    # causes an error message
        $I1 = Compute("3.14159");    # causes an error message
        $I2 = Compute("3,14159");

There are two context flags that control the input and output of decimals.

enterDecimals => "either" (or "," or ".")

This specifies what formats the student is allowed to use to enter a decimal. A value of "either" allows either of the formats to be accepted, while the other two options produce error messages if the wrong form is used.

displayDecimals => "either" (or "," or ".")

This controls how decimals are displayed. When set to "either", the decimal is displayed in whatever format was used to create it. When set to "." or ",", the display is forced to be in the given format regardless of how it was entered.

The AlternateDecimal context has both flags set to "either", so the decimals remain in the format the student entered them, and either form can be used. The AlternateDecimal-Only context has both set to ",", so only the alternate format can be used, and any number will be displayed in the alternate format. The AlternateDecimal-Warning context has both set to ".", so only standard format can be used, and all numbers will be displayed in standard format.

It is possible to set enterDecimals and displayDecimals to different values. For example.

          enterDecimals => "either",
          displayDecimals => ".",

would allow students to enter decimals in either format, but all numebrs would be displayed in standard form.


Because the alternate format allows numbers to be entered using commas rather than periods, this makes the formation of lists harder. For example, 3,5 is the number 3-and-5-tenths, not the list consisting of 3 followed by 5. Because of this ambiguity, the AlternateDecimal contexts also include the semi-colon as a replacement for the comma as a separator. So 3;5 is the list consisting of 3 followed by 5, and 3,1;5.2 is the list consisting of 3.1 and 5.2.

Note that the comma is still available for use as a separator, but this makes things like 3,2,1 tricky, because it is not clear if this is 3.2 followed by 1, or 3.2 times .1, or the list of 3, 2, and 1. To help make this unambiguous, numbers that use a comma as decimal inidcator must have a digit on both sides of the comma. So one tenth would have to be entered as 0,1 not just ,1 (but you can still enter .1. Similarly, You must enter 3,0 or just 3 rather than 3,, even though 3. is acceptable.

With this notation 3,2,1 means the list consisting of 3.2 followed by 1. If you want the list consisting of 3 followed by 2.1, you could use 3, 2,1 since the comma in 3, is not part of the number, so must be a list separator.


If you want to force existing problems to allow (or force, or warn about) the alternate format instead, then create a file named in your course's templates/macros directory, and enter the following in it:


This will alter all the standard contexts to allow students to enter numbers in either format, and will display them using the form that was used to enter them.

You could also do


to cause a warning message to appear when students enter the alternate format.

If you want to force students to enter the alternate format, use


This will force the display of all numbers into the alternate form (so even the ones created in the problem using standard form will show using commas), and will force students to enter their results using commas, though professors answers will still be allowed to be entered in either format (the Default() function converts the first "," to "either", but arranges that the default flags for the answer checker are set to only allow students to enter decimals with commas). This allows you to force comma notation in problems without having to rewrite them.