Link Search Menu Expand Document

Number overview

Various functions to aid in working with numbers.

Added in v0.1.0


Table of contents


utils

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

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('abc'), 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

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