Skip to main content

Arithmetic Functions

Arithmetic functions transform raw numbers into polished, user-friendly displays. Instead of showing "4.7543 stars" or "1234.567 price", you can round, format, and present numbers that look professional and are easy to read.

Why Arithmetic Functions Matter

Raw data problems:

  • Prices like 19.999 instead of $20.00
  • Ratings showing 4.7642 instead of 4.8 stars
  • View counts like 1234567 instead of 1,234,567 views
  • Incomplete pagination when 2.3 pages are needed

What arithmetic functions solve:

  • Currency formatting: Turn 19.99 into $19.99
  • Clean displays: Round 4.7 rating to 5 full stars
  • Readable numbers: Format 1234567 as 1,234,567
  • Smart calculations: Always show enough pages, never show partial items

Rounding Numbers

Make messy decimals clean and presentable.

ceil

Perfect for pagination and ensuring you always have enough space.

ceil(number)

When to use:

  • Pagination (always show enough pages)
  • Minimum quantities needed
  • "Round up to next dollar" pricing

floor

Great for showing only complete units like full stars or available quantities.

floor(number)

When to use:

  • Star ratings (show only full stars)
  • Available complete sets
  • "Round down" discounts

round

Most common rounding for prices, percentages, and general number cleanup.

round(number, precision)

Parameters:

  • precision - Decimal places to keep (0 = whole number)

When to use:

  • Currency (2 decimal places)
  • Percentages (1 decimal place)
  • Clean number displays

Finding Best Values

Compare multiple numbers to find the highest, lowest, or best option.

max

Show the best price, highest rating, or maximum value from multiple options.

max(number1, number2, ...)
max(array_of_numbers)

When to use:

  • Find best rating or highest score
  • Show premium price option
  • Ensure minimum values (ironically!)

min

Show the best deal, lowest price, or minimum value from multiple options.

min(number1, number2, ...)
min(array_of_numbers)

When to use:

  • Find best price or lowest cost
  • Show minimum required value

Making Numbers Readable

Transform raw numbers into polished, professional displays.

number_format

Make large numbers readable with thousands separators and consistent decimal places.

number_format(number, decimals, decimal_point, thousands_separator)

Parameters:

  • decimals - How many decimal places (default: 0)
  • decimal_point - Character for decimals (default: '.')
  • thousands_separator - Character for thousands (default: ',')

When to use:

  • Large numbers (views, downloads, prices)
  • International formatting
  • Consistent decimal display

sprintf

Create exactly formatted strings with placeholders for numbers.

sprintf(format_string, value1, value2, ...)

Format codes:

  • %.2f - Float with 2 decimals
  • %d - Whole number
  • %s - String
  • %% - Literal % symbol

When to use:

  • Exact currency formatting
  • Percentage displays
  • Complex formatted strings

Converting Number Types

Ensure your data is the right type for calculations and displays.

intval

Convert any value to a whole number, removing decimals.

intval(value)

When to use:

  • User IDs and database IDs
  • Quantities and counts
  • Array indices
  • Clean user input

floatval

Convert any value to a decimal number, keeping precision.

floatval(value)

When to use:

  • Price calculations
  • Percentage math
  • Precise measurements
  • Mathematical operations

Working with Lists

Calculate totals and count items in arrays.

sum

Calculate totals from arrays of numbers.

sum(array_of_numbers)

When to use:

  • Shopping cart totals
  • Score calculations
  • Revenue totals
  • Quantity sums

count

Count how many items are in an array or string length.

count(array_or_string)

When to use:

  • Check if array has items
  • Count search results
  • String length checking
  • Loop limits

Common Patterns

Currency Formatting

# US Dollar format
'$' ~ number_format(price, 2)
# Result: "$1,234.50"

# European format
number_format(price, 2, ',', '.') ~ ' €'
# Result: "1.234,50 €"

# Precise formatting
sprintf('$%.2f', price)
# Result: "$19.99"

Percentage Calculations

# Calculate and format percentage
sprintf('%.1f%%', (completed / total) * 100)
# Result: "75.5%"

# Safe percentage (avoid division by zero)
total > 0 ? round((completed / total) * 100, 1) : 0
# Result: 75.5 or 0