Number overview

Various functions to aid in working with numbers.

Added in v0.1.0


Table of contents


1 Typeclass Instances

BoundedSafe

An alternative Bounded instance for numbers which defines top and bottom as Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER respectively.

Signature

export declare const BoundedSafe: Bounded<number>
BoundedSafe :: Bounded number

Example

import { BoundedSafe } from 'fp-ts-std/Number'

assert.strictEqual(BoundedSafe.top, Number.MAX_SAFE_INTEGER)

Added in v0.17.0

EnumInt

An unlawful but predictable instance of Enum for numbers, representing integers between Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER.

Invalid inputs will return None for both succ and pred.

toEnum and fromEnum do not modify the input.

Signature

export declare const EnumInt: Enum<number>
EnumInt :: Enum number

Example

import * as O from 'fp-ts/Option'
import { EnumInt } from 'fp-ts-std/Number'

assert.deepStrictEqual(EnumInt.succ(123), O.some(124))
assert.deepStrictEqual(EnumInt.succ(123.5), O.none)

assert.deepStrictEqual(EnumInt.pred(123), O.some(122))
assert.deepStrictEqual(EnumInt.pred(123.5), O.none)

Added in v0.17.0

3 Functions

add

Add two numbers together.

Signature

export declare const add: (x: number) => Endomorphism<number>
add :: number -> Endomorphism number

Example

import { add } from 'fp-ts-std/Number'

assert.strictEqual(add(2)(3), 5)

Added in v0.1.0

decrement

Decrement a number.

Signature

export declare const decrement: Endomorphism<number>
decrement :: Endomorphism number

Example

import { decrement } from 'fp-ts-std/Number'

assert.strictEqual(decrement(3), 2)

Added in v0.1.0

digits

Returns the individual digits of a number.

Non-numeric characters like -, ., and _ are supported in the input and will be removed.

Values in non-decimal notations are implicitly converted to decimal notation in JavaScript.

An empty array is returned for values like NaN and Infinity.

Signature

export declare const digits: (n: number) => Array<number>
digits :: number -> Array number

Example

import { digits } from 'fp-ts-std/Number'

assert.deepStrictEqual(digits(123), [1, 2, 3])

Added in v0.17.0

divide

Divide the second number (the dividend) by the first number (the divisor).

Signature

export declare const divide: (divisor: number) => Endomorphism<number>
divide :: number -> Endomorphism number

Example

import { divide } from 'fp-ts-std/Number'

assert.strictEqual(divide(2)(4), 2)
assert.strictEqual(divide(4)(2), 0.5)

Added in v0.2.0

floatFromString

Convert a string to a floating point number.

Signature

export declare const floatFromString: (x: string) => Option<number>
floatFromString :: string -> Option number

Example

import { floatFromString } from 'fp-ts-std/Number'
import { some, none } from 'fp-ts/Option'

assert.deepStrictEqual(floatFromString('3.3'), some(3.3))
assert.deepStrictEqual(floatFromString('abc'), none)

Added in v0.1.0

fromString

Convert a string to a number.

Signature

export declare const fromString: (string: string) => Option<number>
fromString :: string -> Option number

Example

import { fromString } from 'fp-ts-std/Number'
import { some, none } from 'fp-ts/Option'

assert.deepStrictEqual(fromString('3'), some(3))
assert.deepStrictEqual(fromString('3.3'), some(3.3))
assert.deepStrictEqual(fromString('0xF'), some(15))
assert.deepStrictEqual(fromString('xyz'), none)

Added in v0.1.0

fromStringWithRadix

Convert a string to a number.

Signature

export declare const fromStringWithRadix: (radix: number) => (string: string) => Option<number>
fromStringWithRadix :: number -> string -> Option number

Example

import { fromStringWithRadix } from 'fp-ts-std/Number'
import { some, none } from 'fp-ts/Option'

assert.deepStrictEqual(fromStringWithRadix(16)('0xF'), some(15))
assert.deepStrictEqual(fromStringWithRadix(16)('xyz'), none)

Added in v0.1.0

increment

Increment a number.

Signature

export declare const increment: Endomorphism<number>
increment :: Endomorphism number

Example

import { increment } from 'fp-ts-std/Number'

assert.strictEqual(increment(3), 4)

Added in v0.1.0

integerFromString

Convert a string to an integer.

Signature

export declare const integerFromString: (string: string) => Option<number>
integerFromString :: string -> Option number

Example

import { integerFromString } from 'fp-ts-std/Number'
import { some, none } from 'fp-ts/Option'

assert.deepStrictEqual(integerFromString('3'), some(3))
assert.deepStrictEqual(integerFromString('3.3'), some(3))
assert.deepStrictEqual(integerFromString('abc'), none)

Added in v0.18.0

isFinite

Check if a number is finite.

Signature

export declare const isFinite: Predicate<number>
isFinite :: Predicate number

Example

import { isFinite } from 'fp-ts-std/Number'

assert.strictEqual(isFinite(123), true)
assert.strictEqual(isFinite(-123), true)
assert.strictEqual(isFinite(Infinity), false)
assert.strictEqual(isFinite(-Infinity), false)

Added in v0.12.0

isNegative

Check if a number is negative.

Signature

export declare const isNegative: Predicate<number>
isNegative :: Predicate number

Example

import { isNegative } from 'fp-ts-std/Number'

assert.strictEqual(isNegative(42), false)
assert.strictEqual(isNegative(0), false)
assert.strictEqual(isNegative(-42), true)

Added in v0.13.0

isNonNegative

Check if a number is non-negative.

Signature

export declare const isNonNegative: Predicate<number>
isNonNegative :: Predicate number

Example

import { isNonNegative } from 'fp-ts-std/Number'

assert.strictEqual(isNonNegative(42), true)
assert.strictEqual(isNonNegative(0), true)
assert.strictEqual(isNonNegative(-42), false)

Added in v0.13.0

isNonPositive

Check if a number is non-positive.

Signature

export declare const isNonPositive: Predicate<number>
isNonPositive :: Predicate number

Example

import { isNonPositive } from 'fp-ts-std/Number'

assert.strictEqual(isNonPositive(42), false)
assert.strictEqual(isNonPositive(0), true)
assert.strictEqual(isNonPositive(-42), true)

Added in v0.13.0

isPositive

Check if a number is positive.

Signature

export declare const isPositive: Predicate<number>
isPositive :: Predicate number

Example

import { isPositive } from 'fp-ts-std/Number'

assert.strictEqual(isPositive(42), true)
assert.strictEqual(isPositive(0), false)
assert.strictEqual(isPositive(-42), false)

Added in v0.13.0

isValid

Check if a number is actually valid. Specifically, all this function is doing is checking whether or not the number is NaN.

Signature

export declare const isValid: Predicate<number>
isValid :: Predicate number

Example

import { isValid } from 'fp-ts-std/Number'

const valid = 123
const invalid = NaN

assert.strictEqual(isValid(valid), true)
assert.strictEqual(isValid(invalid), false)

Added in v0.7.0

mod

Calculate the modulus. See also rem.

Signature

export declare const mod: (divisor: number) => Endomorphism<number>
mod :: number -> Endomorphism number

Example

import { mod } from 'fp-ts-std/Number'

assert.strictEqual(mod(2)(5.5), 1.5)
assert.strictEqual(mod(-4)(2), -2)
assert.strictEqual(mod(5)(-12), 3)

Added in v0.7.0

multiply

Multiply two numbers together.

Signature

export declare const multiply: (x: number) => Endomorphism<number>
multiply :: number -> Endomorphism number

Example

import { multiply } from 'fp-ts-std/Number'

assert.strictEqual(multiply(2)(3), 6)

Added in v0.2.0

negate

Unary negation.

Signature

export declare const negate: Endomorphism<number>
negate :: Endomorphism number

Example

import { negate } from 'fp-ts-std/Number'

assert.strictEqual(negate(42), -42)
assert.strictEqual(negate(-42), 42)

Added in v0.7.0

rem

Calculates the remainder. See also mod.

Signature

export declare const rem: (divisor: number) => Endomorphism<number>
rem :: number -> Endomorphism number

Example

import { rem } from 'fp-ts-std/Number'

assert.strictEqual(rem(2)(5.5), 1.5)
assert.strictEqual(rem(-4)(2), 2)
assert.strictEqual(rem(5)(-12), -2)

Added in v0.7.0

subtract

Subtract the first number (the subtrahend) from the second number (the minuend).

Signature

export declare const subtract: (subtrahend: number) => Endomorphism<number>
subtract :: number -> Endomorphism number

Example

import { subtract } from 'fp-ts-std/Number'

assert.strictEqual(subtract(2)(3), 1)
assert.strictEqual(subtract(3)(2), -1)

Added in v0.2.0

toFinite

If the provided number is Infinity or -Infinity, converts it to the nearest safe finite number.

Signature

export declare const toFinite: Endomorphism<number>
toFinite :: Endomorphism number

Example

import { toFinite } from 'fp-ts-std/Number'

assert.strictEqual(toFinite(123), 123)
assert.strictEqual(toFinite(-123), -123)
assert.strictEqual(toFinite(Infinity), Number.MAX_SAFE_INTEGER)
assert.strictEqual(toFinite(-Infinity), Number.MIN_SAFE_INTEGER)

Added in v0.12.0