Pricing Calculations
A bundle can contain multiple transactions each with their own set of pricing concerns. The bundle itself exposes a function which computes the total cost of all workflows in the bundle. It will return a price to you in the smallest unit of measurement within your active currency code. For example, if your active currency code is USD, the price will be in cents. $1.00 would be represented as 100.
const total = bundle.getTotalSubunits();
Because the value is in subunits you will want to display it in a more human readable way. A function such as the following may be of assistance in doing so.
function formatCentsToDollars(cents) {
// Convert cents to dollars
const dollars = cents / 100;
// Format dollars with currency symbol and commas
const formattedDollars = dollars.toLocaleString("en-US", {
style: "currency",
currency: "USD",
});
return formattedDollars;
}
// Usage example
const cents = 2500;
const formattedAmount = formatCentsToDollars(cents);
console.log(formattedAmount);
For multinational stores this can be a little more complicated as there are quite a few ways currencies need to be represented. In this case we recommend using a third party library such as Dinero.js to simplify the process and make dealing with the values easier. We don't make any assumptions on the approach here as we want to keep our bundle size down. If you have a preferred library or approach we'd love to hear about it.