Number

A Number variable stores numeric values, including both integers (whole numbers) and decimals (floating-point numbers). It's essential for mathematical calculations, counters, prices, quantities, and any numeric data.

When to Use

Use Number variables when you need to:

  • Perform mathematical calculations

  • Store quantities, counts, or indices

  • Manage prices, amounts, or measurements

  • Track progress, scores, or ratings

  • Store API responses containing numeric data

Examples

Integers

42
0
-15
1000

Decimals

Common Use Cases

1. Counters

Track counts like cart items, unread messages, or page numbers:

2. Pricing and Currency

Store and display prices:

3. Progress Tracking

Calculate percentages or progress:

4. Index Tracking

Track current positions in lists or pages:

Working with Numbers

Mathematical Operations

Perform calculations using math function operators:

Math Functions

Use mathematical functions for complex operations. See the Math Operators documentation for all available functions:

  • abs() - Absolute value

  • ceil() - Round up

  • floor() - Round down

  • clamp() - Constrain a value within a range

Comparisons

Compare numbers using logical operator functions:

Formatting Numbers

Currency Formatting

Format as currency:

Best Practices

  • Initialize with sensible defaults: Set initial values to prevent undefined behavior (e.g., 0 for counters, 1 for page numbers)

  • Avoid division by zero: Always check denominators before division

  • Use appropriate precision: Round or fix decimal places when displaying money or percentages

  • Handle edge cases: Consider negative numbers, very large numbers, or zero when performing calculations

  • Type safety: Ensure API responses are actually numbers before using them in calculations

Common Patterns

Incrementing/Decrementing

Clamping Values

Keep values within a range:

Calculating Percentages

Conditional Logic

Last updated