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.999instead of$20.00 - Ratings showing
4.7642instead of4.8 stars - View counts like
1234567instead of1,234,567 views - Incomplete pagination when
2.3 pagesare needed
What arithmetic functions solve:
- Currency formatting: Turn
19.99into$19.99 - Clean displays: Round
4.7rating to5 full stars - Readable numbers: Format
1234567as1,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.
- Description
- Example
- Result
ceil(number)
When to use:
- Pagination (always show enough pages)
- Minimum quantities needed
- "Round up to next dollar" pricing
# Price rounding up
ceil(product_price)
# Shipping boxes needed
ceil(total_items / items_per_box)
# $19.99 → $20
20
# 25 items ÷ 12 per box = 2.08 → 3 boxes
3
floor
Great for showing only complete units like full stars or available quantities.
- Description
- Example
- Result
floor(number)
When to use:
- Star ratings (show only full stars)
- Available complete sets
- "Round down" discounts
# Show full stars only
floor(average_rating)
# Complete sets available
floor(inventory / items_per_set)
# Age in complete years
floor(days_old / 365)
# 4.8 rating → show 4 full stars
4
# 25 items ÷ 6 per set = 4.16 → 4 complete sets
4
# 1,127 days ÷ 365 = 3.08 → 3 complete years
3
round
Most common rounding for prices, percentages, and general number cleanup.
- Description
- Example
- Result
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
# Currency formatting
round(product_price, 2)
# Percentage display
round(completion_rate * 100, 1)
# Clean rating
round(average_rating, 1)
# $19.999 → $19.00
20.00
# 0.847 × 100 = 84.7% → 84.7%
84.7
# 4.67 → 4.7 stars
4.7
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.
- Description
- Example
- Result
max(number1, number2, ...)
max(array_of_numbers)
When to use:
- Find best rating or highest score
- Show premium price option
- Ensure minimum values (ironically!)
# Show highest rating
max(user_rating, expert_rating, community_rating)
# Ensure at least 1 item shown
max(search_results_count, 1)
# Find premium price
max(regular_price, premium_price, vip_price)
# Ratings: 4.2, 4.8, 4.5 → show best
4.8
# 0 results found → show at least 1
1
# Prices: $99, $149, $199 → show highest
199
min
Show the best deal, lowest price, or minimum value from multiple options.
- Description
- Example
- Result
min(number1, number2, ...)
min(array_of_numbers)
When to use:
- Find best price or lowest cost
- Show minimum required value
# Show best price
min(regular_price, sale_price, member_price)
# Limit to maximum 100 items
min(search_results_count, 100)
# Find cheapest shipping
min(standard_shipping, express_shipping, overnight_shipping)
# Prices: $99, $79, $89 → show best deal
79
# 247 results → limit to 100 max
100
# Shipping: $5.99, $12.99, $24.99 → cheapest
5.99
Making Numbers Readable
Transform raw numbers into polished, professional displays.
number_format
Make large numbers readable with thousands separators and consistent decimal places.
- Description
- Example
- Result
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
# View count formatting
number_format(total_views)
# Price with decimals
number_format(product_price, 2)
# European format
number_format(price, 2, ',', '.')
# 1234567 → readable number
"1,234,567"
# 1234.5 → currency format
"1,234.50"
# 1234.50 → European style
"1.234,50"
sprintf
Create exactly formatted strings with placeholders for numbers.
- Description
- Example
- Result
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
# Currency with $ symbol
sprintf('$%.2f', product_price)
# Percentage display
sprintf('%.1f%%', completion_rate * 100)
# Complex formatting
sprintf('%s: %d/%d items (%d%%)', 'Progress', completed, total, percentage)
# 19.5 → exact currency
"$19.50"
# 0.847 × 100 → percentage
"84.7%"
# Multiple values → formatted string
"Progress: 8/10 items (80%)"
Converting Number Types
Ensure your data is the right type for calculations and displays.
intval
Convert any value to a whole number, removing decimals.
- Description
- Example
- Result
intval(value)
When to use:
- User IDs and database IDs
- Quantities and counts
- Array indices
- Clean user input
# Clean user input
intval(user_quantity)
# Remove decimals from ID
intval(wp.post.ID)
# Count as whole number
intval(average_items_per_order)
# "5.7" input → clean quantity
5
# "123abc" → clean ID
123
# 4.8 average → 4 items
4
floatval
Convert any value to a decimal number, keeping precision.
- Description
- Example
- Result
floatval(value)
When to use:
- Price calculations
- Percentage math
- Precise measurements
- Mathematical operations
# Ensure price is decimal
floatval(product_price)
# Clean percentage input
floatval(tax_rate)
# Mathematical precision
floatval(measurement_value)
# "19.99" string → number
19.99
# "0.08" string → decimal
0.08
# "5" → ensure decimal
5.0
Working with Lists
Calculate totals and count items in arrays.
sum
Calculate totals from arrays of numbers.
- Description
- Example
- Result
sum(array_of_numbers)
When to use:
- Shopping cart totals
- Score calculations
- Revenue totals
- Quantity sums
# Shopping cart total
sum([item1_price, item2_price, item3_price])
# Total score
sum([quiz1_score, quiz2_score, quiz3_score])
# Monthly sales
sum(monthly_sales_array)
# Prices: [19.99, 29.99, 9.99] → cart total
59.97
# Scores: [85, 92, 78] → total points
255
# Sales: [1200, 1450, 980] → total revenue
3630
count
Count how many items are in an array or string length.
- Description
- Example
- Result
count(array_or_string)
When to use:
- Check if array has items
- Count search results
- String length checking
- Loop limits
# Count search results
count(search_results)
# Number of categories
count(post_categories)
# Title length
count(post_title)
# [result1, result2, result3] → number found
3
# [category1, category2] → categories assigned
2
# "Hello World" → character count
11
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