Skip to content
Classroom

Format Macros

Built-in functions for formatting numbers, text, and mathematical expressions for display.

Number Formatting

shownum(number, decimals)

Format a number with specified decimal places.

$value = 3.14159;
$formatted = shownum($value, 2);  // Returns "3.14"

$pi = 3.14159;
[number]
answer = $pi;
Question answer is `shownum($pi, 3)`.  // Displays "3.142"

round(number, decimals)

Round and format (also used for calculation).

$result = round(3.14159, 2);  // Returns 3.14

number_format(number, decimals, decimal_separator, thousands_separator)

Format with thousands separator.

$large = 1234567.89;
$formatted = number_format($large, 2, ".", ",");
// Returns "1,234,567.89"

$european = number_format($large, 2, ",", ".");
// Returns "1.234.567,89"

Scientific Notation

sprintf(format, variables)

Format numbers using printf-style formatting.

$value = 0.00043;
$sci = sprintf("%.2e", $value);  // Returns "4.30e-4"

$fixed = sprintf("%.3f", 3.14159);  // Returns "3.142"
$integer = sprintf("%05d", 42);     // Returns "00042"

Percentage Formatting

$decimal = 0.75;
$percentage = $decimal * 100;
$formatted = shownum($percentage, 1) . "%";
// Returns "75.0%"

Currency Formatting

$amount = 1234.50;
$formatted = "$" . number_format($amount, 2);
// Returns "$1,234.50"

Mathematical Expression Formatting

mathquill(expression)

Format mathematical expression for display.

$expr = "x^2 + 2*x + 1";
Display the expression: `mathquill($expr)`

Using Backticks for Math Display

Backticks automatically render AsciiMath:

The solution to `x^2 - 4 = 0` is `x = ±2`.

Solve: `2x + 3 = 11` for x.

Text Formatting

htmlspecialchars(text)

Escape HTML characters for safe display.

$text = '<strong>Be Bold</strong>';
$safe = htmlspecialchars($text);
// Displays literally: <strong>Be Bold</strong>

Examples

Displaying Numerical Answers

$a = rand(1, 5);
$b = rand(1, 5);
$answer = $a * $b;

What is $a × $b?

The answer is `shownum($answer, 0)`.

[number]
answer = $answer;

Displaying Calculated Results

$length = 5.234;
$width = 3.891;
$area = $length * $width;

Area = `shownum($area, 2)` square units

[number]
answer = $area;
tolerance = 0.01;

Formatting Large Numbers

$population = 1234567;
$formatted = number_format($population, 0, ".", ",");

The population is approximately `$formatted` people.

Currency in Questions

$price = 19.99;
$quantity = rand(1, 5);
$total = $price * $quantity;

If each item costs $`shownum($price, 2)` and you buy $quantity items,
the total is $`shownum($total, 2)`.

Percentage Display

$correct = 42;
$total = 50;
$percentage = round(($correct / $total) * 100, 1);

You got `$percentage`% correct (`$correct` out of `$total`).

Scientific Notation for Small Numbers

$coeff = 3.2;
$exponent = -5;
$value = $coeff * pow(10, $exponent);

The value is approximately `sprintf("%.2e", $value)`.

AsciiMath Notation

Display mathematical expressions inside backticks:

Solve: `x^2 - 5x + 6 = 0`

The roots are `x = 2` and `x = 3`.

The formula is: `sqrt(x^2 + y^2) = r`

Trigonometry: `sin^2(x) + cos^2(x) = 1`

Tips

  • Use shownum() for displaying decimal values in questions
  • Use number_format() for large numbers that need commas
  • Use backticks for all mathematical expressions (automatically rendered)
  • Always format answer displays so students see clean values
  • Be consistent — if you show 2 decimal places in the question, expect the same in grading

See Also