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
1000Decimals
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 valueceil()- Round upfloor()- Round downclamp()- 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.,
0for counters,1for 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
Related Documentation
Math Operators - All available mathematical functions
Logical Operators - Comparison and conditional logic
Variables Overview - Learn about all variable types
Last updated