Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/ylin610/units

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/ylin610/units

  • v0.1.0
  • Source
  • Go
  • Socket score

Version published
Created
Source

Description

This package provides type Bytes, which implements fmt.Formatter and can be easily formatted to various human-readable string using std fmt package.

It also has some mathematics methods to modify the value. See example_test.go.

Formatting

The formatting result typically consists of a number and a unit.

There are binary units (kiB, MiB, GiB and TiB), decimal units (kB, MB, GB and TB) and base unit B. Further reading here.

When calculating the output number, binary units are used by default. You can switch to decimal units by adding the # flag.

Why is there no PiB/PB? TiB/TB is sufficient for most cases, and many non-technical people are not familiar with PiB/PB.

Verbs

  • s: format to an integer number and a proper unit.

    fmt.Sprintf("%s", units.Bytes(1023)) // 1023B
    fmt.Sprintf("%s", units.Bytes(1024)) // 1kiB
    fmt.Sprintf("%s", units.Bytes(1025)) // 1kiB
    
  • f: format to a float number and a proper unit, default precision is 1.

    fmt.Sprintf("%f", units.Bytes(1023)) // 1023.0B
    fmt.Sprintf("%f", units.Bytes(1024)) // 1.0kiB
    fmt.Sprintf("%f", units.Bytes(1150)) // 1.1kiB
    
  • v: equal to s.

  • k: format to an integer number with the unit kiB.

    fmt.Sprintf("%k", units.Bytes(1<<9)) // 0B
    fmt.Sprintf("%k", units.Bytes(1<<10)) // 1kiB
    fmt.Sprintf("%k", units.Bytes(1<<20)) // 1024kiB
    
  • m: format to an integer number with the unit MiB.

  • g: format to an integer number with the unit GiB.

  • t: format to an integer number with the unit TiB.

Flags

  • #: use decimal units.

    fmt.Sprintf("%f", 10*units.KiB) // 10.0kiB
    fmt.Sprintf("%#f", 10*units.KiB) // 10.2kB
    
  • : (space) do not output the unit.

    fmt.Sprintf("%s", units.KiB) // 1kiB
    fmt.Sprintf("% s", units.KiB) // 1
    

Sign for numbers (+), left-justify (-) and zero padding (0) are not supported.

Width and precision

The width includes the unit.

fmt.Sprintf("%4s", units.KiB)    //    " 1kiB"
fmt.Sprintf("%8.2f", units.KiB)  // " 1.00kiB"
fmt.Sprintf("% 8.2f", units.KiB) // "    1.00"

FAQs

Package last updated on 20 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc