npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

decimal.js-i18n

v0.3.1

Published

Full internationalization support for decimal.js.

Downloads

142

Readme

decimal.js-i18n

Build Status Code Coverage NPM node-current

Full internationalization support for decimal.js.

  • 🌎 Supports all languages, numbering systems, currencies and units of JavaScript's Intl
  • 🤖 Automatic extension of the Decimal class (custom extender available)
  • 🌞 Expands upon Intl.NumberFormat, pushing it to the limits of decimal.js
  • 🎯 Precision, minimum integer digits, and significance of up to one billion!

Summary

Installation

Install decimal.js-i18n using your preferred package manager. You'll need decimal.js too!

npm i -S decimal.js decimal.js-i18n
yarn add decimal.js decimal.js-i18n
pnpm install decimal.js decimal.js-i18n

Or download a release file with the appropriate version for you.

⇧ back to top

Usage

The easiest way is to use the main module, which auto-extends decimal.js. The other option is to manually augment Decimal with the extend submodule:

TypeScript and ES modules:

import Decimal from "decimal.js-i18n";

Node (CommonJS):

const Decimal = require("decimal.js-i18n");

AMD (RequireJS):

require(["decimal.js"], Decimal => { /* ... */ });

Browsers:

<script src="./scripts/decimal[.min].[m]js" />
<script src="./scripts/decimal-i18n[.min].[m]js" />

Extending manually:

const extend = require("decimal.js-i18n/extend");
const Decimal = extend(require("custom-decimal.js"));

From now on, the method toLocaleString will be available on the Decimal prototype:

pi.toLocaleString('ar', { 
    minimumSignificantDigits: 100, 
    notation: "engineering", 
    style: "unit", 
    unit: "kilometer-per-hour",
});
// Returns: ٣٫١٤١٥٩٢٦٥٣٥٨٩٧٩٣٢٣٨٤٦٢٦٤٣٣٨٣٢٧٩٥٠٢٨٨٤١٩٧١٦٩٣٩٩٣٧٥١٠٥٨٢٠٩٧٤٩٤٤٥٩٢٣٠٧٨١٦٤٠٦٢٨٦٢٠٨٩٩٨٦٢٨٠٣٤٨٢٥٣٤٢١١٧٠٧٠اس٠ كم/س

The backbone of the module, however is the Decimal.Format class — to be used when translating multiple values for an increase in performance — which behaves pretty much like Intl.NumberFormat, without its 20/21 digits limitations and with the increase in precision of decimal.js:

const formatter = new Decimal.Format("lao", {
    currency: "USD",
    minimumFractionDigits: 33, // <- Intl.NumberFormat would throw a `RangeError`
    numberingSystem: "laoo",
    style: "currency",
});

Decimal.set({ precision: 100 });

formatter.format(-1 / 3);
// Returns: US$-໐,໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໐໐໐໐໐໐໐໐໐໐໐໐໐໐໐໐໐

formatter.format(new Decimal(-1).div(3));
// Returns: US$-໐,໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓໓

formatter.format(new Decimal("1.65e51").div(3).cbrt());
// Returns US$໘໑.໙໓໒.໑໒໗.໐໖໐.໐໖໔.໕໘໐,໙໓໗໙໕໐໗໑໒໘໖໒໘໕໗໙໙໗໓໓໓໙໑໐໓໓໘໗໐໔໕໓໗໑໓໑໓໒໘໙໒໑໕໑໓໐໗໕໒໘໒໗໔໓໖໕໒໒໓໓໘໖໑໔໗໐໓໔໙໙໖໘໓໙໓໖໑໔໒໕໑໒໖

Formatting to descriptive parts is also fully implemented:

formatter.formatToParts(Decimal.acos(-1));
// Result: [
//   { type: 'currency', value: 'US$' },
//   { type: 'integer', value: '໓' },
//   { type: 'decimal', value: ',' },
//   {
//     type: 'fraction',
//     value: '໑໔໑໕໙໒໖໕໓໕໘໙໗໙໓໒໓໘໔໖໒໖໔໓໓໘໓໒໗໙໕໐໒໘໘໔໑໙໗໑໖໙໓໙໙໓໗໕໑໐໕໘໒໐໙໗໔໙໔໔໕໙໒໓໐໗໘໑໖໔໐໖໒໘໖໒໐໘໙໙໘໖໒໘໐໓໔໘໒໕໓໔໒໑໑໗໐໖໘'
//   }
// ]

⇧ back to top

Parameters

The constructor for Decimal.Format and Decimal.prototype.toLocaleString are invoked with the following parameters:

locales: string | string[]

(optional) A string with a BCP 47 language tag, or an array of such strings. For the general form and interpretation of the locales argument, see the Intl page on MDN. The system default locale is used when omitted.

options: object

(optional) Object used to configure the behavior of the string localization. If omitted, it uses the same defaults as Intl.NumberFormat. The next section of this document will explain the behavior of each option property as well as their defaults. Descriptions were extracted directly from MDN and W3C and adapted to this document.

⇧ back to top

Options

All configuration values are optional, but a few can become required depending of the style. Some configurations may not be available on all browsers.

compactDisplay

▸ "short" | "long"

Only used when notation is "compact". Takes either "short" (default) or "long".

currency

string

The currency to use in currency formatting. Possible values are the ISO 4217 currency codes, such as "USD" for the US dollar, "EUR" for the euro, or "CNY" for the Chinese RMB — see the Current currency & funds code list. There is no default value; if the style is "currency", the currency property must be provided.

currencyDisplay

▸ "symbol" | "narrowSymbol" | "code" | "name"

How to display the currency in currency formatting. Possible values are:

  • "symbol" to use a localized currency symbol such as €, this is default value;
  • "narrowSymbol" to use a narrow format symbol ("$100" rather than "US$100");
  • "code" to use the ISO currency code;
  • "name" to use a localized currency name such as "dollar".

currencySign

▸ "standard" | "accounting"

In many locales, accounting format means to wrap the number with parentheses instead of appending a minus sign. You can enable this formatting by setting the currencySign option to "accounting". The default value is "standard".

localeMatcher

▸ "best fit" | "lookup"

The locale matching algorithm to use. Possible values are "lookup" and "best fit"; the default is "best fit". For information about this option, see the Intl page on MDN.

notation

▸ "standard" | "scientific" | "engineering" | "compact"

The formatting that should be displayed for the number, the defaults is "standard".

  • "standard" plain number formatting;
  • "scientific" return the order-of-magnitude for formatted number;
  • "engineering" return the exponent of ten when divisible by three;
  • "compact" string representing exponent; defaults to using the "short" form.

numberingSystem

string

A numeral system is a system for expressing numbers. The numberingSystem property helps to represent the different numeral systems used by various countries, regions, and cultures around the world.

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl.Locale.prototype.numberingSystem for more information.

rounding

Decimal.Rounding

Options for rounding modes reflecting the ICU user guide. Used in this plugin as in decimal.js.

signDisplay

▸ "auto" | "never" | "always" | "exceptZero" | "negative"

When to display the sign for the number; defaults to "auto":

  • "always" always display sign;
  • "auto" sign display for negative numbers only;
  • "exceptZero" sign display for positive and negative numbers, but not zero;
  • "negative" sign display for negative numbers only, excluding negative zero;
  • "never" never display sign.

style

▸ "currency" | "unit" | "decimal" | "percent"

The formatting style to use , the default is "decimal".

  • "decimal" for plain number formatting;
  • "currency" for currency formatting;
  • "percent" for percent formatting;
  • "unit" for unit formatting.

trailingZeroDisplay

▸ "auto" | "stripIfInteger" | "lessPrecision"

A string expressing the strategy for displaying trailing zeros on whole numbers. The default is "auto".

  • "auto": keep trailing zeros according to minimumFractionDigits and minimumSignificantDigits;
  • "stripIfInteger": the result with more precision wins a conflict;
  • "lessPrecision": same as "auto", but remove the fraction digits if they are all zero.

unit

string

The unit to use in unit formatting, possible values are core unit identifiers. Only a subset of units from the full list was selected for use in ECMAScript. Pairs of simple units can be concatenated with "-per-" to make a compound unit. There is no default value; if the style is "unit", the unit property must be provided.

unitDisplay

▸ "short" | "long" | "narrow"

The unit formatting style to use in unit formatting, the defaults is "short". Can be "long", "narrow" or "short".

useGrouping

boolean | "auto" | "always" | "false" | "min2" | "true"

Whether to use grouping separators, such as thousands separators or thousand/lakh/crore separators. The default is "auto".

  • "always": display grouping separators even if the locale prefers otherwise;
  • "auto": display grouping separators based on the locale preference, which may also be dependent on the currency;
  • "false": do not display grouping separators;
  • "min2": display grouping separators when there are at least 2 digits in a group;
  • "true": alias for always.

👉 The following properties fall into two groups: maximumFractionDigits, minimumFractionDigits, and minimumIntegerDigits in one group, maximumSignificantDigits and minimumSignificantDigits in the other. If at least one property from the second group is defined, then the first group is ignored.

maximumFractionDigits

number

₁ The maximum number of fraction digits to use. This allows any positive integer value up to 999999999, including; the default for plain number formatting is the larger of minimumFractionDigits and 3; the default for currency formatting is the larger of minimumFractionDigits and the number of minor unit digits provided by the ISO 4217 currency code list (2 if the list doesn't provide that information); the default for percent formatting is the larger of minimumFractionDigits and 0.

minimumFractionDigits

number

₁ The minimum number of fraction digits to use. This allows any positive integer value up to 999999999, including; the default for plain number and percent formatting is 0; the default for currency formatting is the number of minor unit digits provided by the ISO 4217 currency code list (2 if the list doesn't provide that information).

minimumIntegerDigits

number

₁ The minimum number of integer digits to use. This allows any positive natural value up to 1000000000, including; the default is 1.

maximumSignificantDigits

number

₂ The maximum number of significant digits to use. This allows any positive natural value up to 1000000000, including; the default is the larger of minimumFractionDigits and 21.

minimumSignificantDigits

number

₂ The minimum number of significant digits to use. This allows any positive natural value up to 1000000000, including; the default is 1.

⇧ back to top

Links

⇧ back to top

Changelog

v0.3.1 (2022-06-05)

Fix fraction output for all locales

  • Recreate fraction output with better algorithm.
  • Improve export naming.
  • Improve testing according to fraction fixes.
  • Improve testing speed.

v0.3.0 (2022-05-30)

Quality-of-life improvements and fixes.

  • Fix balancing of longer decimal fractions.
  • Improve builder (rewrite in TypeScript).
  • Improve packaging automation.

v0.2.6 (2022-05-26)

Improve distribution, type aliasing, and docs.

  • Add generic typing to notation and style.
  • Established node engine ≥ 12 as a hard requirement.
  • Standardize documentation on doc blocks.
  • Standardize documentation on README.md.
  • Prepare repository for scrutinizer.

v0.2.4-v0.2.5 (2022-05-24)

"What I believe to be a stable version" release.

  • Logo SVG fix;
  • Increase testing;
  • Build script finalized.

v0.2.0-0.2.3 (2022-05-23)

Note: Multiple versions published until many small details were combed through.

TypeScript codebase rewrite.

  • Add extensive randomized testing.
  • Fix many bugs related to notation and style.
  • Better type declarations.

v0.1.0 (2022-05-18)

Note: This was originally written in JS, not TS.

First version.

  • Code base and unit tests created.
  • To be replicated in more environments.
  • To be published after thorough testing.

⇧ back to top

Disclaimer and License

This project IS NOT endorsed or supported by decimal.js, or any of its contributors.

decimal.js-i18n is licensed under the MIT License. See LICENSE.md for more information.

⇧ back to top


Thanks for reading. A big hug from 🇧🇷 to you! :-)