Add up two Dinero objects.
You can only add objects that share the same currency. The function also normalizes objects to the same scale (the highest) before adding them up.
Copy linkParameters
Name | Type | Description | Required |
---|---|---|---|
augend | Dinero<TAmount> | The Dinero object to add to. | Yes |
addend | Dinero<TAmount> | The Dinero object to add. | Yes |
Copy linkCode examples
Copy linkAdd objects
import { dinero, add } from 'dinero.js';
import { USD } from '@dinero.js/currencies';
const d1 = dinero({ amount: 500, currency: USD });
const d2 = dinero({ amount: 100, currency: USD });
add(d1, d2); // a Dinero object with amount 600
Copy linkAdd objects with a different scale
import { dinero, add } from 'dinero.js';
import { USD } from '@dinero.js/currencies';
const d1 = dinero({ amount: 400, currency: USD });
const d2 = dinero({ amount: 104545, currency: USD, scale: 4 });
add(d1, d2); // a Dinero object with amount 144545 and scale 4
Copy linkAdd more than two objects
To retrieve the sum of multiple objects, you can call the add
function multiple times.
import { dinero, add } from 'dinero.js';
import { USD } from '@dinero.js/currencies';
const d1 = dinero({ amount: 300, currency: USD });
const d2 = dinero({ amount: 200, currency: USD });
const d3 = dinero({ amount: 100, currency: USD });
const addMany = (addends) => addends.reduce(add);
addMany([d1, d2, d3]); // a Dinero object with amount 600