Node.js Tutorial: Convert Currency Codes to Symbols in Seconds

Node.js Tutorial: Convert Currency Codes to Symbols in Seconds

Introduction:

Working with currency symbols can be tricky in any programming language, but especially so in Node.js. Fortunately, the currency-symbol-map package provides an easy-to-use solution for handling currency symbols in your Node.js application. Whether you're building a financial application or simply need to display currency symbols in your UI, currency-symbol-map can simplify the process for you.

Installation:

To use currency-symbol-map in your Node.js application, you first need to install it via npm. Open your terminal or command prompt and run the following command:

npm install currency-symbol-map

Usage:

Once you've installed currency-symbol-map, you can use it in your code like this:

const currencySymbolMap = require('currency-symbol-map');

console.log(currencySymbolMap('USD')); // outputs '$'
console.log(currencySymbolMap('EUR')); // outputs '€'
console.log(currencySymbolMap('JPY')); // outputs '¥'

As you can see, using currency-symbol-map is as simple as requiring the package and passing in a currency code as a parameter.

Examples:

Let's take a look at some practical examples of using currency-symbol-map in real-world scenarios.

Example 1: Displaying Currency Symbols in a UI

Suppose you're building an e-commerce website and need to display the currency symbol next to the product price. You can easily accomplish this with currency-symbol-map:

const currencySymbolMap = require('currency-symbol-map');

const productPrice = 10.99;
const currencyCode = 'USD';

const currencySymbol = currencySymbolMap(currencyCode);

console.log(`${currencySymbol}${productPrice}`); 
// outputs '$10.99'

Example 2: Formatting Currency Values

In this example, we have an array of products with different currencies. We use the forEach method to loop through each product and get the currency symbol using the currencySymbolMap package. We then format the price by concatenating the currency symbol with the price rounded to 2 decimal places using the toFixed method. Finally, we log the name of the product and the formatted price to the console.

const currencySymbolMap = require('currency-symbol-map');

const products = [
  { name: 'Product A', price: 10.99, currencyCode: 'USD' },
  { name: 'Product B', price: 9.99, currencyCode: 'EUR' },
  { name: 'Product C', price: 15.99, currencyCode: 'JPY' }
];

products.forEach(product => {
  const { name, price, currencyCode } = product;
  const currencySymbol = currencySymbolMap(currencyCode);
  const formattedPrice = `${currencySymbol}${price.toFixed(2)}`;
  console.log(`${name}: ${formattedPrice}`);
});

// Output:
// Product A: $10.99
// Product B: €9.99
// Product C: ¥15.99

Conclusion:

The currency-symbol-map package provides a simple and effective way to work with currency symbols in your Node.js application. Whether you're working with financial data or simply need to display currency symbols in your UI, currency-symbol-map can help you streamline your development process.

Image by rawpixel.com on Freepik

Did you find this article valuable?

Support Suhail Akhtar Dev Blog by becoming a sponsor. Any amount is appreciated!