๐ŸŒ
Joshuaslate
joshuaslate.com โ€บ blog โ€บ currency-values-in-typescript
Working with Currency Values in TypeScript
November 28, 2022 - A brief introduction to working with currency values, performing arithmetic operations, and locale-sensitive number formatting in TypeScript.
๐ŸŒ
Dinerojs
dinerojs.com
Dinero.js
Create, calculate, and format money in JavaScript and TypeScript.
Discussions

TypeScript import error on node
This code work fine on webpack build, but fail on node TypeError: currency_js_1.default is not a function. I use currency.js in TypeScript project. More on github.com
๐ŸŒ github.com
5
December 12, 2017
How to format numbers as currency strings?
I would like to format a price in JavaScript. I'd like a function which takes a float as an argument and returns a string formatted like this: $ 2,500.00 How can I do this? More on stackoverflow.com
๐ŸŒ stackoverflow.com
typescript - I wan't to format a string of numbers as a currency in javascript but i don't know how to format my string - Stack Overflow
so what i mean is: 1000 should become 10,00 10000 should become 100,00 100000 should become 1000,00 Can some help me out? More on stackoverflow.com
๐ŸŒ stackoverflow.com
types - Are there any good JavaScript currency or decimal classes? - Stack Overflow
There are many libraries to help using currency in javascript,typescript. BIGNUMBER.js It seems the best library, 1,4M of active users, last publish 7 days ago, only 7 open issues, 33 contributors, 6.3k stars. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GitHub
github.com โ€บ scurker โ€บ currency.js
GitHub - scurker/currency.js: A javascript library for handling currencies ยท GitHub
currency.js is a lightweight ~1kb javascript library for working with currency values. It was built to work around floating point issues in javascript.
Starred by 3.4K users
Forked by 147 users
Languages ย  JavaScript 94.7% | TypeScript 5.3%
๐ŸŒ
Js
currency.js.org
currency.js
A small, lightweight javascript library for working with currency values.Download currency.js
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Intl โ€บ NumberFormat
Intl.NumberFormat - JavaScript - MDN Web Docs
js ยท const number = 123456.789; // request a currency format console.log( new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format( number, ), ); // 123.456,79 โ‚ฌ // the Japanese yen doesn't use a minor unit console.log( new Intl.NumberFormat("ja-JP", { style: "currency", currency: "JPY" }).format( number, ), ); // ๏ฟฅ123,457 // limit to three significant digits console.log( new Intl.NumberFormat("en-IN", { maximumSignificantDigits: 3 }).format( number, ), ); // 1,23,000 // Formatting with units console.log( new Intl.NumberFormat("pt-PT", { style: "unit", unit: "kilometer-per-hour", }).format(50), ); // 50 km/h console.log( (16).toLocaleString("en-GB", { style: "unit", unit: "liter", unitDisplay: "long", }), ); // 16 litres ยท
๐ŸŒ
npm
npmjs.com โ€บ package โ€บ ts-money
ts-money - npm
June 22, 2022 - TS Money is a Typescript port of the great js-money package, which is an implementation of Martin Fowlers Money pattern. ... First we need to import the library. ... const TsMoney = require('ts-money') const Money = TsMoney.Money const Currencies ...
      ยป npm install ts-money
    
Published ย  Jun 22, 2022
Version ย  0.4.8
Author ย  Mathew Cormier
๐ŸŒ
xjavascript
xjavascript.com โ€บ blog โ€บ currencyjs-typescript
Mastering Currency.js with TypeScript โ€” xjavascript.com
Incorrect handling of currency ... is a lightweight JavaScript library that simplifies currency management by providing a reliable way to perform arithmetic operations on currency values while avoiding common floating-point errors...
๐ŸŒ
GitHub
github.com โ€บ scurker โ€บ currency.js โ€บ issues โ€บ 73
TypeScript import error on node ยท Issue #73 ยท scurker/currency.js
December 12, 2017 - I import this package like this: import currency from 'currency.js'; currency(1).add(1.1); This code work fine on webpack build, but fail on node TypeError: currency_js_1.default is not a function. I use currency.js in TypeScript project...
Author ย  shepherdwind
Find elsewhere
๐ŸŒ
GitHub
github.com โ€บ half0wl โ€บ money.ts
GitHub - half0wl/money.ts: A TypeScript/JavaScript library to make working with monetary values easier and safer. ยท GitHub
This library aims to make working with monetary values in TypeScript/JavaScript safer and easier. Monetary values always have a currency. They are distinct by currency ($1 is not equivalent to โ‚ฌ1), and each currency has different properties ...
Author ย  half0wl
๐ŸŒ
Kevinleary
kevinleary.net โ€บ blog โ€บ currency-formatting-javascript-typescript
Kevinleary.net: JavaScript Currency Formatting: toLocaleString() Examples
December 20, 2019 - Format numbers as currency strings in JavaScript and TypeScript using the built-in toLocaleString() method. Includes examples for USD formats and international compatibility.
Top answer
1 of 16
2861

Intl.NumberFormat

JavaScript has a number formatter (part of the Internationalization API).

// Create our number formatter.
const formatter = new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'USD',

  // These options can be used to round to whole numbers.
  trailingZeroDisplay: 'stripIfInteger'   // This is probably what most people
                                          // want. It will only stop printing
                                          // the fraction when the input
                                          // amount is a round number (int)
                                          // already. If that's not what you
                                          // need, have a look at the options
                                          // below.
  //minimumFractionDigits: 0, // This suffices for whole numbers, but will
                              // print 2500.10 as $2,500.1
  //maximumFractionDigits: 0, // Causes 2500.99 to be printed as $2,501
});

// Use the formatter with the value of an input.
let input = document.getElementById('amount');
input.addEventListener('keyup', e => {
    document.getElementById('result').innerText = formatter.format(e.target.value);
});
input.dispatchEvent(new Event('keyup'));
<label>
    Amount
    <input id="amount" value="2500">
</label>
Result:
<span id="result"></span>

Use undefined in place of the first argument ('en-US' in the example) to use the system locale (the user locale in case the code is running in a browser). Further explanation of the locale code.

Here's a list of the currency codes.

Intl.NumberFormat vs Number.prototype.toLocaleString

A final note comparing this to the older .toLocaleString. They both offer essentially the same functionality. However, toLocaleString in its older incarnations (pre-Intl) does not actually support locales: it uses the system locale. So when debugging old browsers, be sure that you're using the correct version (MDN suggests to check for the existence of Intl). There isn't any need to worry about this at all if you don't care about old browsers or just use the shim.

Also, the performance of both is the same for a single item, but if you have a lot of numbers to format, using Intl.NumberFormat is ~70 times faster. Therefore, it's usually best to use Intl.NumberFormat and instantiate only once per page load. Anyway, here's the equivalent usage of toLocaleString:

console.log((2500).toLocaleString('en-US', {
  style: 'currency',
  currency: 'USD',
})); /* $2,500.00 */

Some notes on browser support and Node.js

  • Browser support is no longer an issue nowadays with 99+% support globally
  • There is a shim to support it on fossilized browsers (like Internet Explorer 8), should you really need to
  • Node.js before v13 only supports en-US out of the box. One solution is to install full-icu, see here for more information
  • Have a look at CanIUse for more information
2 of 16
1973

Number.prototype.toFixed

This solution is compatible with every single major browser:

  const profits = 2489.8237;

  profits.toFixed(3) // Returns 2489.824 (rounds up)
  profits.toFixed(2) // Returns 2489.82
  profits.toFixed(7) // Returns 2489.8237000 (pads the decimals)

All you need is to add the currency symbol (e.g. "$" + profits.toFixed(2)) and you will have your amount in dollars.

Custom function

If you require the use of , between each digit, you can use this function:

function formatMoney(number, decPlaces, decSep, thouSep) {
    decPlaces = isNaN(decPlaces = Math.abs(decPlaces)) ? 2 : decPlaces,
    decSep = typeof decSep === "undefined" ? "." : decSep;
    thouSep = typeof thouSep === "undefined" ? "," : thouSep;
    var sign = number < 0 ? "-" : "";
    var i = String(parseInt(number = Math.abs(Number(number) || 0).toFixed(decPlaces)));
    var j = (j = i.length) > 3 ? j % 3 : 0;

    return sign +
        (j ? i.substr(0, j) + thouSep : "") +
        i.substr(j).replace(/(\decSep{3})(?=\decSep)/g, "$1" + thouSep) +
        (decPlaces ? decSep + Math.abs(number - i).toFixed(decPlaces).slice(2) : "");
}

document.getElementById("b").addEventListener("click", event => {
  document.getElementById("x").innerText = "Result was: " + formatMoney(document.getElementById("d").value);
});
<label>Insert your amount: <input id="d" type="text" placeholder="Cash amount" /></label>
<br />
<button id="b">Get Output</button>
<p id="x">(press button to get output)</p>

Use it like so:

(123456789.12345).formatMoney(2, ".", ",");

If you're always going to use '.' and ',', you can leave them off your method call, and the method will default them for you.

(123456789.12345).formatMoney(2);

If your culture has the two symbols flipped (i.e., Europeans) and you would like to use the defaults, just paste over the following two lines in the formatMoney method:

    d = d == undefined ? "," : d,
    t = t == undefined ? "." : t,

Custom function (ES6)

If you can use modern ECMAScript syntax (i.e., through Babel), you can use this simpler function instead:

function formatMoney(amount, decimalCount = 2, decimal = ".", thousands = ",") {
  try {
    decimalCount = Math.abs(decimalCount);
    decimalCount = isNaN(decimalCount) ? 2 : decimalCount;

    const negativeSign = amount < 0 ? "-" : "";

    let i = parseInt(amount = Math.abs(Number(amount) || 0).toFixed(decimalCount)).toString();
    let j = (i.length > 3) ? i.length % 3 : 0;

    return negativeSign +
      (j ? i.substr(0, j) + thousands : '') +
      i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands) +
      (decimalCount ? decimal + Math.abs(amount - i).toFixed(decimalCount).slice(2) : "");
  } catch (e) {
    console.log(e)
  }
};

document.getElementById("b").addEventListener("click", event => {
  document.getElementById("x").innerText = "Result was: " + formatMoney(document.getElementById("d").value);
});
<label>Insert your amount: <input id="d" type="text" placeholder="Cash amount" /></label>
<br />
<button id="b">Get Output</button>
<p id="x">(press button to get output)</p>

๐ŸŒ
DEV Community
dev.to โ€บ thesmilingsloth โ€บ building-a-type-safe-money-handling-library-in-typescript-3o44
Building a Type-Safe Money Handling Library in TypeScript - DEV Community
January 10, 2025 - A robust, type-safe money handling utility for JavaScript/TypeScript applications with support for both fiat and cryptocurrency operations.
๐ŸŒ
Medium
medium.com โ€บ @padamghimire โ€บ creating-a-currency-formatter-in-typescript-c45c6cf199d7
Creating a Currency Formatter in TypeScript | by Padam Ghimire | Medium
March 11, 2024 - currency: This specifies the currency code. Here, we use 'NPR' for Nepalese Rupee. 4. format Method: This method formats the given number according to the options specified in the Intl.NumberFormat constructor and returns the formatted string.
๐ŸŒ
npm
npmjs.com โ€บ package โ€บ country-to-currency
country-to-currency - npm
3 weeks ago - Maps country codes (ISO 3166-1 alpha-2) to their default currency codes (ISO 4217). โšก Just 2.3 KB (uncompressed), no external dependencies. ๐ŸŽฏ Work with browsers, NodeJS, and DenoJS. JavaScript and TypeScript.
      ยป npm install country-to-currency
    
Published ย  Mar 17, 2026
Version ย  2.0.3
Author ย  Thiago Delgado Pinto
๐ŸŒ
Reddit
reddit.com โ€บ r/reactjs โ€บ standard npm library for currency conversion and formatting along with currency symbol
r/reactjs on Reddit: Standard npm library for currency conversion and formatting along with currency symbol
June 2, 2024 -

i am working on an ecommerce website, where i need to display the product price based on the local currency of the user's region, so i need to convert the price of product from usd to that local currency and i also need that local currency symbol for display purpose, is there any standard library for that in reactjs

๐ŸŒ
DEV Community
dev.to โ€บ ambrookhuis โ€บ building-a-currency-input-with-react-and-typescript-2jd7
Building a currency input with React and TypeScript - DEV Community
July 16, 2020 - interface Props { value: number | undefined; updateValue: (value: number | undefined) => void; } const CurrencyInput: React.FC<Props> = ({ value: valueFromProp, updateValue, }) => { return ( <input type="text" value={currentValue} onChange={handleChange} /> ) }