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:

Type
Size (bits)
Range
Usage

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  // BigDecimal

⚡ 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:

Configuration Options:

  • highPrecisionMath - Enable/disable automatic BigDecimal usage (default: true)

  • mathPrecision - Number of significant digits (default: 128)

  • mathRoundingMode - Rounding strategy: HALF_UP, HALF_DOWN, CEILING, FLOOR, etc.

  • Use the ortus.boxlang.runtime.types.util.MathUtil to change other parameters for high precision math.

💰 PrecisionEvaluate for Complex Expressions

For string-based math expressions requiring guaranteed precision, use precisionEvaluate(), or if you disable high precision math:

Learn more about precisionEvaluate()

🧮 Basic Arithmetic

Try it online!

🏷️ 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:

Try it online!

📚 Mathematical Built-In Functions (BIFs)

BoxLang provides comprehensive mathematical functions organized by category:

🔢 Basic Math Operations

Function
Purpose
Example

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

Function
Purpose
Example

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

Function
Purpose
Example

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

Function
Purpose
Example

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

Function
Purpose
Example

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

Function
Purpose
Example

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

Function
Purpose
Example

precisionEvaluate()

Evaluate with BigDecimal

precisionEvaluate(\"1/3 + 2/3\")1.0

⚙️ 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:

Learn more about parseNumber()

Radix/Base Systems: A radix (or base) is the number of unique digits used to represent numbers. Common systems: decimal (base 10: 0-9), binary (base 2: 0-1), octal (base 8: 0-7), hexadecimal (base 16: 0-9, A-F).

Try it online!

✅ Is it a number?

Use isNumeric() to check if a value can be converted to a number:

Try it online!

🔁 Looping with Numbers

Numbers are commonly used in loops for repetition and iteration:

BoxLang provides multiple looping constructs. You can also iterate over arrays, structures, queries, and objects. See the Loop component reference for details.

Try it online!

Last updated

Was this helpful?