Skip to main content

Number Utils

NumberUtil provides helpers for number manipulation operations. This utility class offers static methods to format, transform, and compare numeric values in a consistent and efficient manner.

nextPowerOfTwo

Calculates the next power of two that is greater than or equal to the given number.

static nextPowerOfTwo(source:number):number
const textureWidth = 600;
const powerOfTwoWidth = NumberUtil.nextPowerOfTwo(textureWidth);
console.log(powerOfTwoWidth);
// Output: 1024

roundValue

Rounds a number to the specified precision.

static roundValue(value:number, precision:number = 0.01):number
const price = 10.126;
const roundedPrice = NumberUtil.roundValue(price);
console.log(roundedPrice);
// Output: 10.13

const score = 95.671;
const roundedScore = NumberUtil.roundValue(score, 0.1);
console.log(roundedScore);
// Output: 95.7

approxEquals

Checks if two numbers are approximately equal within a specified epsilon (tolerance).

static approxEquals(value_1:number, value_2:number, epsilon:number = 0.001):boolean
const position1 = 10.0001;
const position2 = 10.0005;
const arePositionsEqual = NumberUtil.approxEquals(position1, position2);
console.log(arePositionsEqual);
// Output: true

const precise1 = 5.00001;
const precise2 = 5.00003;
const arePreciselyEqual = NumberUtil.approxEquals(precise1, precise2, 0.0001);
console.log(arePreciselyEqual);
// Output: true

const value1 = 7.5;
const value2 = 7.6;
const areValuesEqual = NumberUtil.approxEquals(value1, value2);
console.log(areValuesEqual);
// Output: false

clampValue

Constrains a number to be within a specified range.

static clampValue(value:number, min:number, max:number):number
const currentHealth = -10;
const clampedHealth = NumberUtil.clampValue(currentHealth, 0, 100);
console.log(clampedHealth);
// Output: 0 (clamped to the minimum)

const rawScore = 150;
const clampedScore = NumberUtil.clampValue(rawScore, 0, 100);
console.log(clampedScore);
// Output: 100 (clamped to the maximum)

const volume = 75;
const clampedVolume = NumberUtil.clampValue(volume, 0, 100);
console.log(clampedVolume);
// Output: 75 (unchanged as it's already within range)

interpolateValue

Linearly interpolates between two numbers based on a progress value.

static interpolateValue(value_1:number, value_2:number, progress:number):number
const startPosition = 0;
const endPosition = 100;
const centerPosition = NumberUtil.interpolateValue(startPosition, endPosition, 0.5);
console.log(centerPosition);
// Output: 50

addLeadingZero

Formats a number as a string with leading zeros to reach the specified length.

static addLeadingZero(value:number, length:number):string
const score = 5;
const formattedScore = NumberUtil.addLeadingZero(score, 3);
console.log(formattedScore);
// Output: 005

addTrailingZero

Formats a number as a string with trailing zeros to reach the specified length.

static addTrailingZero(value:number, length:number):string
const value = 25;
const formattedValue = NumberUtil.addTrailingZero(value, 4);
console.log(formattedValue);
// Output: 2500

splitWithDots

Formats a number as a string with dots as thousand separators.

static splitWithDots(value:number):string
const balance = 1000000;
const formattedBalance = NumberUtil.splitWithDots(balance);
console.log(formattedBalance);
// Output: 1.000.000