8bits 中文文档教程

发布于 9年前 浏览 26 项目主页 更新于 3年前

8bits Build Status

用于处理和转换字节值的 Javascript 库

表示字节大小的主要方式有两种:SI 单位(十进制/Base 10 / 10^3)和 IEC 单位(二进制/Base 2 / 2^10)。 8bits 支持十进制和二进制两种格式,默认格式为十进制。

Install

$ npm install --save 8bits

Usage

var byte = require('8bits');

byte(789);
//=> 789 B

byte(1000);
//=> 1 kB

// Setting the number of decimal places
byte(1024, {digits: 2});
//=> 1.02 kB

// Using binary instead of decimal
byte(1024, {digits: 2, binary: true});
//=> 1.00 KB

// Converting from one prefix to another
byte(500000, {from: 'kB', to: 'MB'});
//=> 500 MB

byte(512000, {from: 'KB', to: 'MB', binary: true});
//=> 500 MB

// Formatting the return value
byte(512000000, {format: '%s - %s'});
//=> 512 - kB

// Using custom unit values
byte(2000000, {
  units: {
    MB: 'Megabytes'
  }
});
//=> 2 Megabytes

// All together
byte(2000000000000, {
  from: 'MB',
  to: 'YB',
  format: '%s (%s)',
  binary: true,
  digits: 6,
  units: {
    YB: 'yoda bytes'
  }
});
//=> 0.000002 (yoda bytes)

Todo

  • Retrieve byte value from human-readable value (i.e. 1 MB => 1048576)
  • Include API in Readme

Useful Information

// Prefixes for multiples of bits (b) or bytes (B)
//      Decimal                   Binary
// ================================================
// Value    Metric   |  Value    JEDEC       IEC
// 1000     k kilo   |  1024     K kilo    Ki  kibi
// 1000^2   M mega   |  1024^2   M mega    Mi  mebi
// 1000^3   G giga   |  1024^3   G giga    Gi  gibi
// 1000^4   T tera   |  1024^4   – –       Ti  tebi
// 1000^5   P peta   |  1024^5   – –       Pi  pebi
// 1000^6   E exa    |  1024^6   – –       Ei  exbi
// 1000^7   Z zetta  |  1024^7   – –       Zi  zebi
// 1000^8   Y yotta  |  1024^8   – –       Yi  yobi

License

麻省理工学院 © Andrew Kennedy

8bits Build Status

A Javascript library for manipulating and converting byte values

There are two primary ways to represent byte sizes: SI units (decimal / Base 10 / 10^3) and IEC units (binary / Base 2 / 2^10). 8bits supports both of decimal and binary formats, with the default format being decimal.

Install

$ npm install --save 8bits

Usage

var byte = require('8bits');

byte(789);
//=> 789 B

byte(1000);
//=> 1 kB

// Setting the number of decimal places
byte(1024, {digits: 2});
//=> 1.02 kB

// Using binary instead of decimal
byte(1024, {digits: 2, binary: true});
//=> 1.00 KB

// Converting from one prefix to another
byte(500000, {from: 'kB', to: 'MB'});
//=> 500 MB

byte(512000, {from: 'KB', to: 'MB', binary: true});
//=> 500 MB

// Formatting the return value
byte(512000000, {format: '%s - %s'});
//=> 512 - kB

// Using custom unit values
byte(2000000, {
  units: {
    MB: 'Megabytes'
  }
});
//=> 2 Megabytes

// All together
byte(2000000000000, {
  from: 'MB',
  to: 'YB',
  format: '%s (%s)',
  binary: true,
  digits: 6,
  units: {
    YB: 'yoda bytes'
  }
});
//=> 0.000002 (yoda bytes)

Todo

  • Retrieve byte value from human-readable value (i.e. 1 MB => 1048576)
  • Include API in Readme

Useful Information

// Prefixes for multiples of bits (b) or bytes (B)
//      Decimal                   Binary
// ================================================
// Value    Metric   |  Value    JEDEC       IEC
// 1000     k kilo   |  1024     K kilo    Ki  kibi
// 1000^2   M mega   |  1024^2   M mega    Mi  mebi
// 1000^3   G giga   |  1024^3   G giga    Gi  gibi
// 1000^4   T tera   |  1024^4   – –       Ti  tebi
// 1000^5   P peta   |  1024^5   – –       Pi  pebi
// 1000^6   E exa    |  1024^6   – –       Ei  exbi
// 1000^7   Z zetta  |  1024^7   – –       Zi  zebi
// 1000^8   Y yotta  |  1024^8   – –       Yi  yobi

License

MIT © Andrew Kennedy

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文