Currency is complex, JavaScript is not enough to correctly complete the representation of currency, just like we use JS CSS, xx=”14px” which is obviously ugly, but there are already css.px (14), much more formal, more can avoid us to make some mistakes, today to share with you dinero.js. Dinero.js is a JavaScript library that allows you to express the value of currency, as well as perform mutations, transformations, comparisons, formatting, and generally making manipulation of currency in your application easier and more secure.

  • Github is a huge repository of great projects. In fact, it is not necessarily the projects with many stars that deserve attention. There are many small and beautiful projects that we can pay attention to and timely fork. Standing on the shoulders of predecessors to build wheels or directly applied to the project, so as not to be afraid of 🙅🏻♀️ roll. We strive to use the shortest article every day, the shortest time, to bring you the greatest benefits.

ℹ️ dinero.js v2 is currently in its alpha phase. For V1, check the V1 branches and documentation.


Dinero. Js for details

1. Go straight to code demo code

import { CNY, USD } from '@dinero.js/currencies';
import {
  dinero,
  add,
  toUnit,
  down,
  multiply,
  toSnapshot,
  greaterThanOrEqual,
  convert,
} from 'dinero.js';
// Create the base currency object
const d1 = dinero({ amount: 35.currency: CNY });
const d2 = dinero({ amount: 200.currency: CNY });
// Compare money with units
const hasFreeShipping = greaterThanOrEqual(d2, d1);
console.log('⏰', hasFreeShipping);
// Format the money amount 💰 0.35
console.log('💰', toUnit(d1, { digits: 2.round: down }));
// Calculate money amount 💰 35 + 200 = 235
console.log('🐻', add(d1, d2).toJSON());
// Calculate 5.5% VAT rate
const tax = multiply(d2, 0.055);
const total = add(d2, tax);
console.log('🐶', toSnapshot(total));
// Currency conversion
const rates = { USD: { amount: 1549.scale: 4}};console.log('🇨 🇳', convert(d2, USD, rates).toJSON());
Copy the code

2. The following documents can give you a more detailed understanding of it

  • Getting started quickly and learn about dinero.js
  • Core Concepts Understand the framework principles
  • The Guides delved into specific syntax use cases
  • Full list of more detailed API parameters
  • FAQ Common answers to questions that you may encounter

Full documentation official documentation solution

3. Front-end technical AC group of grounding gas

  • There are more than 10,000 front end students gathered here, irregular open classes, special classes. Daily morning front morning reading class, afternoon workplace topic portal

Thank you again for taking the time to read this little article, if you have anything to say please leave it in the comments below ~

By Lao Yuan September 06, 2021