Numbers
Integers and floats to rule the world!
There are two basic kinds of numbers in BoxLang: integers (whole numbers) and floats (have a decimal point). BoxLang provides sophisticated number handling with automatic type promotion and high-precision arithmetic.
🔢 Number Types
BoxLang uses Java's numeric types internally:
Integer
32
-2,147,483,648 to 2,147,483,647
Whole numbers
Long
64
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Large whole numbers
Double
64
±4.9E-324 to ±1.7976931348623157E308
Decimal numbers (15-16 digits precision)
BigDecimal
Arbitrary
Unlimited precision
High-precision calculations, currency
BigInteger
Arbitrary
Unlimited size
Very large whole numbers
🎯 Type Promotion & Type Contagion
BoxLang is smart enough to automatically promote numeric types at runtime with no need for explicit casting. This is called type contagion - when you mix different numeric types in an operation, BoxLang automatically promotes to the most appropriate type:
// Integer operations stay as integers
result = 10 + 5 // Integer: 15
// Mixing integer and decimal promotes to decimal
result = 10 + 5.5 // Double: 15.5
// Large numbers automatically become BigDecimal/BigInteger
result = 111111111111111111111111111 + 222222222222222222222222222
// BigInteger: 333333333333333333333333333
// Decimal operations use BigDecimal for precision
result = 0.1 + 0.2 // BigDecimal: 0.3 (not 0.30000000000000004!)
// Mixed operations promote to the highest precision type
intVal = 10
doubleVal = 5.5
bigDecVal = 100.123456789
result = intVal + doubleVal + bigDecVal // BigDecimalNo Precision Loss! BoxLang automatically uses BigDecimal for decimal operations to avoid floating-point precision issues. For example, (0.1 + 0.2) correctly returns 0.3, not 0.30000000000000004 like in other languages.
⚡ High Precision Math
High precision math is enabled by default in BoxLang! This means BoxLang automatically uses BigDecimal for decimal arithmetic to maintain precision:
You can configure high precision math in your boxlang.json configuration:
💰 PrecisionEvaluate for Complex Expressions
For string-based math expressions requiring guaranteed precision, use precisionEvaluate(), or if you disable high precision math:
precisionEvaluate() works with +, -, *, /, (), and MOD operators. Other operators (^, %, \) will use standard precision.
🧮 Basic Arithmetic
🏷️ The numeric Type
When typing function arguments and return values, use the numeric type instead of specific types like integer or double. This allows BoxLang's type promotion to work naturally:
📚 Mathematical Built-In Functions (BIFs)
BoxLang provides comprehensive mathematical functions organized by category:
🔢 Basic Math Operations
abs()
Absolute value
abs(-5) → 5
ceiling()
Round up to integer
ceiling(4.3) → 5
floor()
Round down to integer
floor(4.7) → 4
round()
Round to nearest integer/decimal
round(4.567, 2) → 4.57
fix()
Convert to integer (truncate)
fix(4.7) → 4
int()
Closest smaller integer
int(4.7) → 4
sgn()
Sign of number (-1, 0, 1)
sgn(-5) → -1
max()
Maximum of two numbers
max(10, 20) → 20
min()
Minimum of two numbers
min(10, 20) → 10
📐 Trigonometric Functions
sin()
Sine (radians)
sin(pi()/2) → 1
cos()
Cosine (radians)
cos(0) → 1
tan()
Tangent (radians)
tan(pi()/4) → 1
asin()
Arc sine (inverse)
asin(1) → 1.5708
acos()
Arc cosine (inverse)
acos(1) → 0
atn()
Arc tangent (inverse)
atn(1) → 0.7854
pi()
Value of π
pi() → 3.14159...
📊 Exponential & Logarithmic
exp()
Exponential (e^x)
exp(1) → 2.71828
log()
Natural logarithm
log(10) → 2.302585
log10()
Base-10 logarithm
log10(100) → 2
sqr()
Square root
sqr(16) → 4
🎲 Random Number Generation
rand()
Random float (0 to 1)
rand() → 0.742351
randRange()
Random integer in range
randRange(1, 100) → 42
randomize()
Seed random generator
randomize(12345)
🔧 Number Manipulation
incrementValue()
Increment integer part
incrementValue(4.7) → 5.7
decrementValue()
Decrement integer part
decrementValue(4.7) → 3.7
formatBaseN()
Convert to base-N string
formatBaseN(255, 16) → \"FF\"
inputBaseN()
Parse base-N string
inputBaseN(\"FF\", 16) → 255
🔨 Bitwise Operations
bitAnd()
Bitwise AND
bitAnd(12, 10) → 8
bitOr()
Bitwise OR
bitOr(12, 10) → 14
bitXor()
Bitwise XOR
bitXor(12, 10) → 6
bitNot()
Bitwise NOT
bitNot(12) → -13
bitSHLN()
Shift left
bitSHLN(5, 2) → 20
bitSHRN()
Shift right
bitSHRN(20, 2) → 5
bitMaskSet()
Set bits in mask
bitMaskSet(0, 0, 3) → 7
bitMaskClear()
Clear bits in mask
bitMaskClear(7, 0, 2) → 4
bitMaskRead()
Read bits from mask
bitMaskRead(7, 0, 2) → 3
🎯 High Precision
precisionEvaluate()
Evaluate with BigDecimal
precisionEvaluate(\"1/3 + 2/3\") → 1.0
Complete Reference: For detailed documentation of each function, visit the Math BIF Reference.
⚙️ Member Functions
All numeric BIFs can be called as member functions on number variables:
Java Number Methods
Since BoxLang numbers are Java objects, you also have access to Java methods:
🔄 Casting/Parsing
While BoxLang automatically handles type promotion, you can explicitly convert values to numbers:
The parseNumber() function converts strings to numbers in different numeral systems:
✅ Is it a number?
Use isNumeric() to check if a value can be converted to a number:
🔁 Looping with Numbers
Numbers are commonly used in loops for repetition and iteration:
🔗 Related Documentation
RangesOperatorsMath BIF Reference - Complete list of mathematical functions
Numeric Type Reference - Member function documentation
Java Number API - Native Java methods
Java BigDecimal API - High precision arithmetic
Java Math API - Java Math class methods
Last updated
Was this helpful?
