-
Notifications
You must be signed in to change notification settings - Fork 0
Number Operations
Phantom.js provides 25 number operations for mathematical calculations, rounding, validation, and more.
- Basic Math Operations
- Rounding Operations
- Advanced Math Operations
- Validation Operations
- Utility Operations
Add two numbers.
phantom.numbers.operation.add(5, 3);
// Output: 8
phantom.numbers.operation.add(10.5, 2.3);
// Output: 12.8Subtract two numbers.
phantom.numbers.operation.subtract(10, 3);
// Output: 7
phantom.numbers.operation.subtract(5, 10);
// Output: -5Multiply two numbers.
phantom.numbers.operation.multiply(5, 3);
// Output: 15
phantom.numbers.operation.multiply(2.5, 4);
// Output: 10Divide two numbers.
phantom.numbers.operation.divide(10, 2);
// Output: 5
phantom.numbers.operation.divide(7, 2);
// Output: 3.5
phantom.numbers.operation.divide(10, 0);
// Throws: Error("Invalid operation")Calculate modulo (remainder).
phantom.numbers.operation.mod(10, 3);
// Output: 1
phantom.numbers.operation.mod(15, 5);
// Output: 0Round to specified decimal places.
phantom.numbers.operation.round(3.14159, 2);
// Output: 3.14
phantom.numbers.operation.round(3.5, 0);
// Output: 4Round up to nearest integer.
phantom.numbers.operation.ceil(3.1);
// Output: 4
phantom.numbers.operation.ceil(-3.1);
// Output: -3Round down to nearest integer.
phantom.numbers.operation.floor(3.9);
// Output: 3
phantom.numbers.operation.floor(-3.1);
// Output: -4Truncate decimal part.
phantom.numbers.operation.truncate(3.7);
// Output: 3
phantom.numbers.operation.truncate(-3.7);
// Output: -3Format number with fixed decimal places (returns string).
phantom.numbers.operation.toFixed(3.14159, 2);
// Output: "3.14"
phantom.numbers.operation.toFixed(5, 2);
// Output: "5.00"Calculate power (base^exponent).
phantom.numbers.operation.pow(2, 3);
// Output: 8
phantom.numbers.operation.pow(5, 2);
// Output: 25Calculate square root.
phantom.numbers.operation.sqrt(4);
// Output: 2
phantom.numbers.operation.sqrt(9);
// Output: 3
phantom.numbers.operation.sqrt(-1);
// Throws: Error("Invalid operation")Get absolute value.
phantom.numbers.operation.abs(-5);
// Output: 5
phantom.numbers.operation.abs(5);
// Output: 5Get the minimum of two numbers.
phantom.numbers.operation.min(5, 10);
// Output: 5Get the maximum of two numbers.
phantom.numbers.operation.max(5, 10);
// Output: 10Parse a value to a number (strict validation).
phantom.numbers.operation.parse("123");
// Output: 123
phantom.numbers.operation.parse("abc");
// Throws: Error("Invalid operation")Check if a value is a valid number.
phantom.numbers.operation.isNumber("123");
// Output: true
phantom.numbers.operation.isNumber("abc");
// Output: false
phantom.numbers.operation.isNumber(null);
// Output: true (Number(null) = 0)Check if number is even.
phantom.numbers.operation.isEven(2);
// Output: true
phantom.numbers.operation.isEven(3);
// Output: falseCheck if number is odd.
phantom.numbers.operation.isOdd(3);
// Output: true
phantom.numbers.operation.isOdd(2);
// Output: falseCheck if number is positive.
phantom.numbers.operation.isPositive(5);
// Output: true
phantom.numbers.operation.isPositive(-5);
// Output: falseCheck if number is negative.
phantom.numbers.operation.isNegative(-5);
// Output: true
phantom.numbers.operation.isNegative(5);
// Output: falseCheck if number is zero.
phantom.numbers.operation.isZero(0);
// Output: true
phantom.numbers.operation.isZero(5);
// Output: falseGenerate random number in range.
phantom.numbers.operation.random(0, 10);
// Output: Random number between 0 (inclusive) and 10 (exclusive)
// Example: 7.234567
phantom.numbers.operation.random();
// Output: Random number between 0 and 1Generate random integer in range.
phantom.numbers.operation.randomInt(1, 10);
// Output: Random integer between 1 and 10 (inclusive)
// Example: 7Check if number is between two values (inclusive).
phantom.numbers.operation.between(5, 1, 10);
// Output: true
phantom.numbers.operation.between(0, 1, 10);
// Output: falseClamp value between min and max.
phantom.numbers.operation.clamp(5, 1, 10);
// Output: 5 (within range)
phantom.numbers.operation.clamp(0, 1, 10);
// Output: 1 (clamped to min)
phantom.numbers.operation.clamp(15, 1, 10);
// Output: 10 (clamped to max)Get sign of number.
phantom.numbers.operation.sign(5);
// Output: 1 (positive)
phantom.numbers.operation.sign(-5);
// Output: -1 (negative)
phantom.numbers.operation.sign(0);
// Output: 0 (zero)var price = phantom.maps.channel.get("price");
var quantity = phantom.maps.channel.get("quantity");
var taxRate = 0.08; // 8%
// Calculate
var subtotal = phantom.numbers.operation.multiply(
phantom.numbers.operation.parse(price),
phantom.numbers.operation.parse(quantity)
);
var tax = phantom.numbers.operation.multiply(subtotal, taxRate);
var total = phantom.numbers.operation.add(subtotal, tax);
// Format for display
var formattedTotal = phantom.numbers.operation.toFixed(total, 2);
// Output: "21.60"var age = phantom.maps.channel.get("age");
if (phantom.numbers.operation.isNumber(age)) {
var ageNum = phantom.numbers.operation.parse(age);
if (phantom.numbers.operation.between(ageNum, 18, 65)) {
// Valid age range
phantom.maps.channel.save("validAge", true);
}
}var value = 100;
var percentage = 15;
var result = phantom.numbers.operation.divide(
phantom.numbers.operation.multiply(value, percentage),
100
);
// Output: 15 (15% of 100)var randomId = phantom.numbers.operation.randomInt(100000, 999999);
// Output: Random 6-digit number
var paddedId = phantom.strings.operation.leftPad(
randomId.toString(),
"0",
8
);
// Output: "00123456"-
Always validate before parsing - Use
isNumberbeforeparse - Handle division by zero - Check denominator before dividing
-
Use appropriate rounding - Choose
round,ceil,floor, ortruncatebased on needs -
Clamp values to ranges - Use
clampto ensure values stay within bounds -
Format for display - Use
toFixedwhen displaying numbers to users
- String Operations - Convert numbers to strings
- Map Operations - Store and retrieve numeric data
- Examples - Real-world number processing examples
Phantom - A product of David Labs.