Click here for v1.x documentation.
Dinero.js
Dinero.js version

How do I calculate a percentage?

There are two ways to calculate a percentage with Dinero.js: using allocate or multiply.

For example, if you need to calculate 15% of a Dinero object, you can split it with allocate.

import { dinero, allocate } from 'dinero.js';
import { USD } from '@dinero.js/currencies';

const price = dinero({ amount: 5000, currency: USD });

const [tax] = allocate(price, [15, 85]);

You can do the same with multiply.

import { dinero, multiply } from 'dinero.js';
import { USD } from '@dinero.js/currencies';

const price = dinero({ amount: 5000, currency: USD });

const tax = multiply(price, { amount: 15, scale: 2 });

If you need this often, you can abstract it into your own percentage function.

function percentage(dineroObject, share, scale = 0) {
  const power = scale + 1;
  const rest = 100 ** power - share;
  const [chunk] = allocate(price, [share, rest], { scale });

  return chunk;
}