Giter VIP home page Giter VIP logo

cryptoformat's Introduction

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 €"

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)

cryptoformat's People

Contributors

ernsheong avatar superoo7 avatar tmlee avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.