Utility to simplify conversion of digital units of measurement
Using npm
$ npm install digital-unit-converter --saveUsing yarn
$ yarn add digital-unit-converterCommonJS
const { DataUnit } = require('digital-unit-converter');ES6
import { DataUnit } from 'digital-unit-converter';JavaScript: convert 10 megabytes (MB) to bytes (B)
let converted = DataUnit.MEGABYTE.toBytes(10);
console.log(converted); // 10000000
// or use convert() instead
converted = DataUnit.BYTE.convert(10, DataUnit.MEGABYTE);
console.log(converted); // 10000000TypeScript: convert 1 gibibyte (GiB) to mebibytes (MiB)
let converted: number = DataUnit.GIBIBYTE.toMebibytes(1);
console.log(converted); // 1024
// or use convert() instead
converted = DataUnit.MEBIBYTE.convert(1, DataUnit.GIBIBYTE);
console.log(converted); // 1024The following data units are currently supported.
| DataUnit | Term | Abbreviation | Unit Type | Number of Bytes |
|---|---|---|---|---|
| BIT | bit | b | - | 0.125 |
| BYTE | byte | B | - | 1 |
| KILOBYTE | kilobyte | kB | Decimal | 103 |
| KIBIBYTE | kibibyte | KiB | Binary | 210 |
| MEGABYTE | megabyte | MB | Decimal | 106 |
| MEBIBYTE | mebibyte | MiB | Binary | 220 |
| GIGABYTE | gigabyte | GB | Decimal | 109 |
| GIBIBYTE | gibibyte | GiB | Binary | 230 |
▪ Static Readonly BIT: DataUnit
Bit (b). Data unit representing a binary digit
Defined in: index.ts:10
▪ Static Readonly BYTE: DataUnit
Byte (B). Data unit representing 8 bits
Defined in: index.ts:25
▪ Static Readonly GIBIBYTE: DataUnit
Gibibyte (GiB). Binary data unit representing 1,0243 bytes
Defined in: index.ts:115
▪ Static Readonly GIGABYTE: DataUnit
Gigabyte (GB). Decimal data unit representing 1,0003 bytes
Defined in: index.ts:100
▪ Static Readonly KIBIBYTE: DataUnit
Kibibyte (KiB). Binary data unit representing 1,024 bytes
Defined in: index.ts:55
▪ Static Readonly KILOBYTE: DataUnit
Kilobyte (kB). Decimal data unit representing 1,000 bytes
Defined in: index.ts:40
▪ Static Readonly MEBIBYTE: DataUnit
Mebibyte (MiB). Binary data unit representing 1,0242 bytes
Defined in: index.ts:85
▪ Static Readonly MEGABYTE: DataUnit
Megabyte (MB). Decimal data unit representing 1,0002 bytes
Defined in: index.ts:70
▸ convert(val: number, unit: DataUnit): number
Converts the value (val) in the specified data unit (unit) to this unit
| Name | Type | Description |
|---|---|---|
val |
number | value to convert |
unit |
DataUnit | data unit of val |
Returns: number
converted value in this unit
Defined in: index.ts:259
▸ toBits(val: number): number
Convert a value in this unit to bits (b)
| Name | Type | Description |
|---|---|---|
val |
number | value to convert to bits |
Returns: number
converted bit value
Defined in: index.ts:178
▸ toBytes(val: number): number
Convert a value in this unit to bytes (B)
| Name | Type | Description |
|---|---|---|
val |
number | value to convert to bytes |
Returns: number
converted byte value
Defined in: index.ts:188
▸ toGibibytes(val: number): number
Convert a value in this unit to gibibytes (GiB)
| Name | Type | Description |
|---|---|---|
val |
number | value to convert to gibibytes |
Returns: number
converted gibibyte value
Defined in: index.ts:248
▸ toGigabytes(val: number): number
Convert a value in this unit to gigabytes (GB)
| Name | Type | Description |
|---|---|---|
val |
number | value to convert to gigabytes |
Returns: number
converted gigabyte value
Defined in: index.ts:238
▸ toKibibytes(val: number): number
Convert a value in this unit to kibibytes (KiB)
| Name | Type | Description |
|---|---|---|
val |
number | value to convert to kibibytes |
Returns: number
converted kibibyte value
Defined in: index.ts:208
▸ toKilobytes(val: number): number
Convert a value in this unit to kilobytes (kB)
| Name | Type | Description |
|---|---|---|
val |
number | value to convert to kilobytes |
Returns: number
converted kilobyte value
Defined in: index.ts:198
▸ toMebibytes(val: number): number
Convert a value in this unit mebibytes (MiB)
| Name | Type | Description |
|---|---|---|
val |
number | value to convert to mebibytes |
Returns: number
converted mebibyte value
Defined in: index.ts:228
▸ toMegabytes(val: number): number
Convert a value in this unit to megabytes (MB)
| Name | Type | Description |
|---|---|---|
val |
number | value to convert to megabytes |
Returns: number
converted megabyte value
Defined in: index.ts:218