Number conversions
Conversion functions and constants for numbers: Int(x), Float(x), Int.MAX, Number.isInteger and friends. The types themselves are lowercase: int, uint, float, and int8 to float32 for packed storage.
Always available
No import needed. These names are in scope in every file.
Number conversion
ts
Number(value: any): float64Coerce value to the most appropriate numeric form: an int when the value fits in int32, a uint when it fits in uint32, otherwise a float64. Strings parse as decimal numbers; non-numeric strings yield NaN. Booleans become 0 or 1; objects are NaN.
Carries static helpers: Number.isFinite, Number.isNaN, Number.isInteger, Number.parseInt, Number.parseFloat.
Number.toFixed
ts
toFixed(digits?: int): stringInstance surface shared by every numeric type (Int8 through Float): a method declared here works on all of them.
Number.toString
ts
toString(radix?: int): stringFormat the number as a string; radix selects the base.
Number.isFinite
ts
static isFinite(value: any): booleantrue for an int, uint, or finite float; false for NaN, infinities, and non-numbers.
Number.isNaN
ts
static isNaN(value: any): booleantrue only for a float NaN; any non-number is false.
Number.isInteger
ts
static isInteger(value: any): booleantrue for an int or uint, or a finite float with no fraction; false otherwise.
Example
ts
console.log(Number.isInteger(5)); // true
console.log(Number.isInteger(5.5)); // falseNumber.parseInt
ts
static parseInt(value: string, radix?: int): intParse the leading integer of a string (decimal); NaN when it does not start with a number. A numeric argument truncates toward zero.
Number.parseFloat
ts
static parseFloat(value: string): float64Parse a decimal number from the start of a string; NaN when it does not start with a number.
Boolean class
ts
class BooleanFinal: it cannot be subclassed.
Convert value to its truthiness. Strings, arrays, maps, and objects are truthy; 0, NaN, "", null, and undefined are falsy.
Boolean.callable
ts
callable(value: any): booleanConvert value to its truthiness; Boolean(0) is false.
Boolean.valueOf
ts
valueOf(): booleanReturn the boolean itself.
Other members of Boolean
ts
toString(): stringInt conversion
ts
Int(value: any, radix?: int): intConvert value to a 32-bit signed integer. Floats truncate toward zero; strings parse in radix (default auto-detect: 0x hex, 0b binary, 0o octal, otherwise decimal). Throws on out-of-range or non-coercible input. Use tryInt for the non-throwing variant.
Int.MAX
ts
static readonly MAX: intLargest Int value.
Int.MIN
ts
static readonly MIN: intSmallest Int value.
Int.wrap
ts
static wrap(value: float): intTwo's-complement wrap of value (fraction truncated toward zero) into int. Never raises for a finite number.
Int.clamp
ts
static clamp(value: float): intSaturate value (fraction truncated toward zero) to the int range. Never raises for a finite number.
Int8 conversion
ts
Int8(value: any, radix?: int): intConvert value to an 8-bit signed integer in [-128, 127]. Throws RangeError if outside that range. radix applies to string inputs.
Int8.MAX
ts
static readonly MAX: intLargest Int8 value.
Int8.MIN
ts
static readonly MIN: intSmallest Int8 value.
Int8.wrap
ts
static wrap(value: float): intTwo's-complement wrap of value (fraction truncated toward zero) into int8. Never raises for a finite number.
Int8.clamp
ts
static clamp(value: float): intSaturate value (fraction truncated toward zero) to the int8 range. Never raises for a finite number.
Int16 conversion
ts
Int16(value: any, radix?: int): intConvert value to a 16-bit signed integer in [-32768, 32767].
Int16.MAX
ts
static readonly MAX: intLargest Int16 value.
Int16.MIN
ts
static readonly MIN: intSmallest Int16 value.
Int16.wrap
ts
static wrap(value: float): intTwo's-complement wrap of value (fraction truncated toward zero) into int16. Never raises for a finite number.
Int16.clamp
ts
static clamp(value: float): intSaturate value (fraction truncated toward zero) to the int16 range. Never raises for a finite number.
Int32 conversion
ts
Int32(value: any, radix?: int): intConvert value to a 32-bit signed integer (alias of the platform int; same range as Int).
Int32.MAX
ts
static readonly MAX: intLargest Int32 value.
Int32.MIN
ts
static readonly MIN: intSmallest Int32 value.
Int32.wrap
ts
static wrap(value: float): int32Two's-complement wrap of value (fraction truncated toward zero) into int32. Never raises for a finite number.
Int32.clamp
ts
static clamp(value: float): int32Saturate value (fraction truncated toward zero) to the int32 range. Never raises for a finite number.
Float conversion
ts
Float(value: any): float64Convert value to a 64-bit float (alias of Float64). Strings parse with strtod; non-coercible input throws.
Float.MAX
ts
static readonly MAX: float64Largest Float value.
Float.MIN_POSITIVE
ts
static readonly MIN_POSITIVE: float64Smallest positive normal Float value.
Float.EPSILON
ts
static readonly EPSILON: float64Difference between 1 and the next larger Float value.
Float.NaN
ts
static readonly NaN: float64Not-a-number value.
Float.POSITIVE_INFINITY
ts
static readonly POSITIVE_INFINITY: float64Positive infinity.
Float.NEGATIVE_INFINITY
ts
static readonly NEGATIVE_INFINITY: float64Negative infinity.
Float64 conversion
ts
Float64(value: any): float64Convert value to a 64-bit float. Same as Float; the explicit name is preferred when the bit width matters.
Float64.MAX
ts
static readonly MAX: float64Largest Float64 value.
Float64.MIN_POSITIVE
ts
static readonly MIN_POSITIVE: float64Smallest positive normal Float64 value.
Float64.EPSILON
ts
static readonly EPSILON: float64Difference between 1 and the next larger Float64 value.
Float64.NaN
ts
static readonly NaN: float64Not-a-number value.
Float64.POSITIVE_INFINITY
ts
static readonly POSITIVE_INFINITY: float64Positive infinity.
Float64.NEGATIVE_INFINITY
ts
static readonly NEGATIVE_INFINITY: float64Negative infinity.
Float32 conversion
ts
Float32(value: any): floatConvert value to a 32-bit float. The result is rounded to float32 precision and returned as a float.
Float32.MAX
ts
static readonly MAX: floatLargest Float32 value.
Float32.MIN_POSITIVE
ts
static readonly MIN_POSITIVE: floatSmallest positive normal Float32 value.
Float32.EPSILON
ts
static readonly EPSILON: floatDifference between 1 and the next larger Float32 value.
Float32.NaN
ts
static readonly NaN: floatNot-a-number value.
Float32.POSITIVE_INFINITY
ts
static readonly POSITIVE_INFINITY: floatPositive infinity.
Float32.NEGATIVE_INFINITY
ts
static readonly NEGATIVE_INFINITY: floatNegative infinity.
Float32.round
ts
static round(value: float): floatRound value to float32 precision. Overflow becomes Infinity or -Infinity; never raises.
UInt conversion
ts
UInt(value: any, radix?: int): uintConvert value to a 32-bit unsigned integer (alias of UInt32). Throws RangeError if negative or above UINT32_MAX.
UInt.MAX
ts
static readonly MAX: uintLargest UInt value.
UInt.MIN
ts
static readonly MIN: uintSmallest UInt value.
UInt.wrap
ts
static wrap(value: float): uintTwo's-complement wrap of value (fraction truncated toward zero) into uint. Never raises for a finite number.
UInt.clamp
ts
static clamp(value: float): uintSaturate value (fraction truncated toward zero) to the uint range. Never raises for a finite number.
UInt8 conversion
ts
UInt8(value: any, radix?: int): uintConvert value to an 8-bit unsigned integer in [0, 255].
UInt8.MAX
ts
static readonly MAX: uintLargest UInt8 value.
UInt8.MIN
ts
static readonly MIN: uintSmallest UInt8 value.
UInt8.wrap
ts
static wrap(value: float): uintTwo's-complement wrap of value (fraction truncated toward zero) into uint8. Never raises for a finite number.
UInt8.clamp
ts
static clamp(value: float): uintSaturate value (fraction truncated toward zero) to the uint8 range. Never raises for a finite number.
UInt16 conversion
ts
UInt16(value: any, radix?: int): uintConvert value to a 16-bit unsigned integer in [0, 65535].
UInt16.MAX
ts
static readonly MAX: uintLargest UInt16 value.
UInt16.MIN
ts
static readonly MIN: uintSmallest UInt16 value.
UInt16.wrap
ts
static wrap(value: float): uintTwo's-complement wrap of value (fraction truncated toward zero) into uint16. Never raises for a finite number.
UInt16.clamp
ts
static clamp(value: float): uintSaturate value (fraction truncated toward zero) to the uint16 range. Never raises for a finite number.
UInt32 conversion
ts
UInt32(value: any, radix?: int): uintConvert value to a 32-bit unsigned integer in [0, 4294967295].
UInt32.MAX
ts
static readonly MAX: uintLargest UInt32 value.
UInt32.MIN
ts
static readonly MIN: uintSmallest UInt32 value.
UInt32.wrap
ts
static wrap(value: float): uint32Two's-complement wrap of value (fraction truncated toward zero) into uint32. Never raises for a finite number.
UInt32.clamp
ts
static clamp(value: float): uint32Saturate value (fraction truncated toward zero) to the uint32 range. Never raises for a finite number.
Generated from lang/types/builtin_numerics.d.tks in the ThicketScript repository.