Skip to content

convertDataUnit

Converts data between different units (e.g., bits, bytes, kilobytes, etc.).

Supports binary prefix and decimal prefix.

Binary prefixes are based on powers of 2 (e.g., 1 KiB = 2^10 bytes) defined by the International Electrotechnical Commission (IEC), while decimal prefixes are part of the International System of Units (SI) based on powers of 10 (e.g., 1 kB = 10^3 bytes).

Added in v0.0.2

Usage

ts
import { convertDataUnit } from 'parsnip-kit'

convertDataUnit(1, 'B', 'bit', 'binary') // 8
convertDataUnit(1, 'B', 'bit', 'decimal') // 8

convertDataUnit(1024, 'B', 'KB', 'binary') // 1
convertDataUnit(1000, 'B', 'KB', 'decimal') // 1

API

Type Parameter

Arguments

ArgTypeOptionalDefaultDescription
valuenumberfalseundefinedThe value of the data size to be converted
fromDataUnitfalseundefinedThe original unit of the data size
toDataUnitfalseundefinedThe target data unit to convert to
prefix'binary' | 'decimal'falseundefinedThe type of prefix to use in conversion (binary or decimal), defaults to 'binary'

Returns

Type
number

Reference

DataUnit