Socket
Socket
Sign inDemoInstall

@coingecko/cryptoformat

Package Overview
Dependencies
0
Maintainers
3
Versions
38
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @coingecko/cryptoformat

Javascript library to format and display cryptocurrencies and fiat


Version published
Weekly downloads
4.3K
decreased by-16.73%
Maintainers
3
Created
Weekly downloads
 

Readme

Source

Cryptoformat

npm version Build Status License: MIT

cryptoformat is used by CoinGecko (https://www.coingecko.com) to format crypto and fiat values.

Often an altcoin can be worth much less than $0.01 USD, and thus we need to format this value by providing more decimal places in the formatting to prevent losing precious information.

cryptoformat also tries to handle different locales and currency formatting by deferring the work to the browser's Intl.NumberFormat. If Intl.NumberFormat is not supported by the browser, cryptoformat provides a primitive fallback for currency display.

Install

npm i @coingecko/cryptoformat

Usage

import { formatCurrency } from "@coingecko/cryptoformat";

formatCurrency(123, "USD", "en");
// "$123.00"

formatCurrency(0.00123, "USD", "en");
// "$0.00123000"

// Provide raw = true to remove formatting and symbol
formatCurrency(0.00123, "USD", "en", true);
// "0.00123000"

formatCurrency(123400, "IDR", "id");
// "Rp123.400"

formatCurrency(123400, "EUR", "de");
// "123.400 €"

// Provide noDecimal = true to explicitly remove decimal for numbers above > 1.0
formatCurrency(4000.23, "USD", "en", false, true);
// "$4,000"

// Provide number of decimal places or significant figures
formatCurrency(1.234, "USD", "en", false, { decimalPlaces: 2 });
// "$1.23"

formatCurrency(1234, "USD", "en", false, { significantFigures: 2 });
// "$1,200"

// Provide number of significant figures only up to specified number of decimal places 
formatCurrency(0.1234, "USD", "en", false, { decimalPlaces: 2, significantFigures: 3 });
// "$0.12"

cryptoformat tries to cache formatters for reuse internally. If same locale and currency is used, the cached formatter will be used.

Known Issues

  1. Intl.NumberFormat does not always behave consistently across browsers. cryptoformat does some manual overrides in order to ensure that "MYR123.00" is displayed as "RM123.00", for example.
  2. Given that country detection for locale is quite hard to do, e.g. "en-MY", cryptoformat does not try to do country sniffing. It is the responsibility of the caller to provide that if possible, but providing only "en" should also work for the most part, but not perfectly: users in different regions may expect a different formatting for the same language.

Development

Deployment

npm run build && npm run submit

File Sturcture

|- lib
    |- index.js (Build with babel)
|- src
    - index.js (Main Code)
    - index.d.ts (Types for TypeScript)
    - test.js (Test with Jest)

FAQs

Last updated on 23 Jan 2024

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc