Numerical Display Functions

The following list includes both WebAssign-specific and commonly-used Perl functions that are available when creating questions.

Rounding, Significant Digits, Scientific Notation  

Operation

Usage

Notes

Round to specified decimal places

decform(n, 
places)

Returns n rounded to the specified number of decimal places.

Round up (next greater integer)

ceil(n)

Returns the first integer greater than n, for example, ceil(23.2) is 24, ceil(-23.7) is -23.

Round down (next lesser integer)

floor(n)

Returns the first integer less than n, for example, floor(23.7) is 23, floor(-23.2) is - 24.

Return integer only

int(n)

Returns only the integer component of n, for example, int(23.7) is 23, int(-23.2) is - 23.

Get significant digits

sigfigs(n)

Returns the number of significant digits in n, ignoring trailing zeros.

Display significant digits

sigform(n, 
digits)

Returns n rounded to the specified number of significant digits. Uses e notation if needed, for example, 2.30e+04.

Get decimal places

decfigs(n)

Returns a power of 10 that represents the smallest significant decimal place in n. Ignores trailing zeros.

Display scientific notation

scinot(n, 
digits)

Returns n formatted as scientific notation with the specified number of significant digits, for example 2.31 × 104. Do not use results for additional computation.

Display e notation

sciform(n, 
digits)

Returns n in e notation with up to the specified number of significant digits after the decimal point, for example 2.31e+04.

Display of Numerical Values

The listed functions are mostly used in Question or Solution to format values for display.  

Operation

Usage

Notes

Reduced fraction

fraction(n,d)

Returns the fraction n/d in reduced form, for example, 1/2 or 1. If used in Answer, specify $FRACTION=1.

Rounded integer

decdisplay(n)

Returns n rounded to the nearest integer. Uses e notation when |n| ≥ 1e+05.

Add commas

commas(n)

Returns n with commas between digit groups to the left of the decimal point, for example, 12,345.678901. Do not use results for additional computation.

Add spaces

spaces(n)

Returns n with spaces between digit groups on both sides of the decimal point, for example, 12 345.678 901. Do not use results for additional computation.

Display as words

spell_number(n)

Returns the integer part of n in words, for example, negative twenty-three. Does not add commas.

Display as words (ordinal)

spell_ordinal(n)

Returns the integer part of n as an ordinal number in words, for example, thirteenth. Does not add commas.