Integer and Decimal—for financial calculations that require exact arithmetic without overflow or rounding errors.
Integer Type
TheInteger type provides 256-bit signed arbitrary precision integers.
Structure
Creating Integers
Arithmetic Operations
Unchecked operations (panic on overflow):Comparisons
Conversions
When to Use Integer
Use Integer for:- Token balances
- Large financial calculations
- Vote counts
- Pool liquidity amounts
- Any value that might exceed u64 (18.4 quintillion)
- Exact arithmetic requirements
- Small counters
- Block heights
- Array indices
- When you know the value fits in 64 bits
Decimal Type
TheDecimal type provides arbitrary precision decimals for calculations requiring fractional values.
Structure
Creating Decimals
Operations
When to Use Decimal
Use Decimal for:- Price calculations
- Percentage calculations
- Logarithmic operations
- Scientific calculations
Example: Token Contract
Example: AMM Pool Math
Choosing Between Checked and Unchecked
Use checked operations when:- Working with user inputs
- Complex calculations where overflow is possible
- You want specific error messages for overflow
- Financial calculations requiring exact results
- You’ve already validated the operation is safe
- Performance is critical
- The overflow would indicate a bug (panic is appropriate)
Common Patterns
Safe Division
Square Root for LP Tokens
Percentage Calculations
Comparison Utilities
Quick Reference
Number Types and Ranges
Integer- 256-bit signed arbitrary precision integers
- Range:
±115_792_089_237_316_195_423_570_985_008_687_907_853_269_984_665_640_564_039_457 - Maximum value: 2^256 - 1
- Arbitrary precision decimals with up to 18 decimal places
- Range:
±(2^256 - 1) / 10^18 - Full range:
±115_792_089_237_316_195_423_570_985_008_687_907_853_269_984_665_640_564_039_457.584_007_913_129_639_936
Arithmetic Operations
Both types support basic arithmetic operations (add, sub, mul, div) and comparisons.
Unchecked (using operators):
Advanced Operations
Integer:.sqrt()- Square root (returnsResult<Integer, Error>)
.log10()- Base-10 logarithm (returnsResult<Decimal, Error>)- Additional operations to be expanded