Number
Numbers in Sessel represent numeric values — integers and decimals. Both types interoperate seamlessly in expressions; you rarely need to distinguish between them.
Literal syntax
| Form | Example | Description |
|---|---|---|
| Integer | 42, -7, 0 |
Whole numbers, positive or negative |
| Decimal | 3.14, -0.5 |
Numbers with a fractional part |
42
3.14
-7
Arithmetic operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ |
Addition | 10 + 3 |
13 |
- |
Subtraction | 10 - 3 |
7 |
* |
Multiplication | 10 * 3 |
30 |
/ |
Division | 10 / 4 |
2 |
${[itemprop="price"]}.sum() / ${[itemprop="price"]}.count()
(${[itemprop="subtotal"]}.sum() + 5) * 1.1
Integer division of two integers returns an integer (truncating toward zero). When either operand is a float, the result is a float: 10 / 4 produces 2, but 10.0 / 4 produces 2.5.
Comparison operators
Numbers support all six comparison operators plus the spaceship operator. Comparison results are booleans; the spaceship result is an integer.
| Operator | Meaning |
|---|---|
== |
Equal to |
!= |
Not equal to |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal to |
<= |
Less than or equal to |
<=> |
Spaceship (three-way compare): returns -1, 0, or 1 |
${div.item}.count() > 3
${[itemprop="price"]}.sum() >= 100
${[itemprop="stock"]}.first().value().Integer() == 0
The spaceship operator <=> is primarily useful in sort comparator lambdas:
${li}.sort((a, b) => a.text().Number() <=> b.text().Number())
Methods
.String()
Converts a number to its string representation.
${div.item}.count().String()
Returns: string
Useful when a template or string method requires a string rather than a number.
.abs()
Returns the absolute value of a number.
(-5).abs() // 5
5.abs() // 5
(-3.14).abs() // 3.14
Returns: the same numeric type (integer stays integer, float stays float).
.floor()
Rounds a number down toward negative infinity. Integer values are returned unchanged. Float values are rounded down and returned as integers.
3.7.floor() // 3
(-3.2).floor() // -4
42.floor() // 42
Returns: integer
.ceil()
Rounds a number up toward positive infinity. Integer values are returned unchanged. Float values are rounded up and returned as integers.
3.2.ceil() // 4
(-3.7).ceil() // -3
42.ceil() // 42
Returns: integer
.round()
Rounds a number to the nearest integer, with ties rounding away from zero. Integer values are returned unchanged.
3.5.round() // 4
3.4.round() // 3
(-3.5).round() // -4
42.round() // 42
Returns: integer
How numbers arise
Numbers appear in Sessel expressions from several sources:
Literals
42 + 8
Coercion from strings — coercion methods that parse numeric content:
| Method | Parses as | Returns |
|---|---|---|
.Number() |
Smart parse: integer for "42", float for "3.14" |
number or null |
.Integer() |
Whole number only | integer (throws if unparseable) |
.Float() |
Always decimal | float (throws if unparseable) |
${[itemprop="price"]}.first().value().Number()
${[itemprop="quantity"]}.first().value().Integer()
${[itemprop="rating"]}.first().value().Float()
Only .Number() returns null if the string cannot be parsed — .Integer() and .Float() throw a type error instead. Use ?? to provide a fallback on .Number():
${[itemprop="price"]}.first().value().Number() ?? 0
List aggregation methods — these operate on a list of elements and return a number:
| Method | Returns |
|---|---|
.count() |
Number of elements in the list (always integer) |
.sum() |
Sum of all element values parsed as numbers |
.min() |
Smallest numeric value in the list |
.max() |
Largest numeric value in the list |
.sum(), .min(), and .max() preserve the integer type when all inputs are integers. If any input is a float, the result is a float.
String length — .count() on a string returns the number of characters:
${h1}.first().text().count()
Number coercion
Coercion methods convert between number subtypes:
| Method | On String | On Integer | On Float |
|---|---|---|---|
.Number() |
Smart parse: "42" → integer, "3.14" → float |
Returns unchanged | Returns unchanged |
.Integer() |
Parses whole number; fails on decimals | Returns unchanged | Truncates toward zero: 3.14 → 3 |
.Float() |
Parses as float: "42" → 42.0 |
Widens: 42 → 42.0 |
Returns unchanged |
"42".Number() // 42 (integer)
"3.14".Number() // 3.14 (float)
3.14.Integer() // 3
42.Float() // 42.0
Random number generation
Type namespaces provide static random() methods for generating random numbers.
Integer.random(min, max)
Returns a random integer in the inclusive range [min, max]. Both arguments must be integers.
Integer.random(1, 6) // a random die roll: 1, 2, 3, 4, 5, or 6
Integer.random(0, 100) // a random integer from 0 to 100 inclusive
Float.random(min, max)
Returns a random float in the half-open range [min, max). Both arguments must be numeric.
Float.random(0.0, 1.0) // a random float in [0.0, 1.0)
Float.random(1, 10) // integer args are promoted to float
Number.random(min, max)
Infers the return type from argument types. If both arguments are integers, returns an integer. If either is a float, returns a float.
Number.random(1, 10) // integer result (both args are integer)
Number.random(1.0, 10.0) // float result (both args are float)
Number.random(1, 10.0) // float result (mixed args)
Range validation: if min > max, the bounds are silently swapped. If min == max, min is returned directly.