TNumber Class
The TNumber
class provides utility methods for handling numbers, including parsing, constraining, percentage calculation, and various checks.
Usage
Importing the TNumber Class
Using the TNumber Class from the t
Object
Methods
Parse
parse(value: string | number): ParseReturn<T>
Parses a string or number into a number.
Example:
Constrain
constrain(value: number, min: number, max: number): number
Constrains a number between a minimum and maximum value.
Example:
Percent
percent(value: number, total: number): number
Calculates the percentage of a number relative to a total.
Example:
ToSafe
toSafe(value: number): number
Returns the original value for a safe integer, or 0 for a non-safe integer.
Example:
When the value is not safe, the result is 0:
Number Checks
The TNumber
class includes a set of number-related checks provided by the INumberChecks
interface.
Even
is.even(value: number): boolean
Checks if a number is even.
Example:
Odd
is.odd(value: number): boolean
Checks if a number is odd.
Example:
Between
is.between(value: number, min: number, max: number): boolean
Checks if a number is between a specified range.
Example:
Safe
is.safe(value: number): boolean
Checks if a number is safe to use (not NaN, not Infinity, within safe integer range).
Example: