Skip to content

Amount

configure

1.0.0

Configures global defaults for Amount operations such as default currency and rounding behavior. This is a static method that affects all Amount instances. It has aliases named config() and setConfig() for convenience.

Amount.configure(/config/);
// or using aliases
Amount.config(/config/);
Amount.setConfig(/config/);
  • Sets global configuration for all Amount instances
  • Configures default currency and rounding behavior
  • Has convenient config() and setConfig() aliases
  • Affects operations like multiply, divide, and convert
  • Persists until explicitly changed or reset
  • Static method - called on the Amount class
  • Simple and intuitive API
  • Comprehensive testing
ArgTypeDefault ValueRequired
configAmountConfig-Yes

Configure global defaults for Amount operations using the static configure() method.

import { Amount } from '@inpulse-ui/utils';
// Set global configuration
Amount.configure({
defaultCurrency: 'USD',
defaultRounding: 'bank'
});
// Now all Amount operations use these defaults
const amount1 = Amount.set(100); // Uses USD as default currency
const result = amount1.multiply(1.5); // Uses bank rounding by default

Using the config() alias:

// Equivalent to configure()
Amount.config({
defaultCurrency: 'EUR',
defaultRounding: 'math'
});

Using the setConfig() alias:

// Also equivalent to configure()
Amount.setConfig({
defaultCurrency: 'GBP',
defaultRounding: 'bank'
});

Configure default currency:

// Set EUR as default currency
Amount.configure({
defaultCurrency: 'EUR'
});
// Now Amount.set() uses EUR when no currency is specified
const price = Amount.set(99.99); // Creates amount in EUR
console.log(price.getCurrency()); // 'EUR'

Configure default rounding:

// Set mathematical rounding as default
Amount.configure({
defaultRounding: 'math'
});
// All multiply/divide operations use math rounding by default
const amount = Amount.set(10, 'USD');
const result = amount.multiply(1.333); // Uses math rounding

Imagine an e-commerce platform operating in Brazil:

Amount.configure({ defaultCurrency: 'BRL', defaultLocale: 'pt-BR' });
const total = Amount.set(299.90).add(25.50).format(); // "R$ 325,40"

Now, imagine its usage in a US-based SaaS application:

Amount.configure({ defaultCurrency: 'USD', defaultLocale: 'en-US' });
const monthly = Amount.set(49.99).divideBy(30).multiply(15); // Prorated billing

Now, imagine its usage in a German payment application:

Amount.configure({ defaultCurrency: 'EUR', defaultLocale: 'de-DE', defaultRounding: 'bank' });
const vatPrice = Amount.set(100).multiply(1.19); // German VAT

You could config at the application initialization:

// Set up global defaults at application start
function initializeApp() {
Amount.configure({
defaultCurrency: 'USD',
defaultRounding: 'bank'
});
// Rest of application initialization
}
// Later in the application
const orderAmount = Amount.set(299.99); // Automatically uses USD
const tax = orderAmount.multiply(0.08); // Uses bank rounding

Here’s how you might configure based on user region:

// Configure based on user's region
const userRegion = getUserRegion();
switch (userRegion) {
case 'US':
Amount.config({
defaultCurrency: 'USD',
defaultRounding: 'math'
});
break;
case 'EU':
Amount.config({
defaultCurrency: 'EUR',
defaultRounding: 'bank'
});
break;
case 'UK':
Amount.config({
defaultCurrency: 'GBP',
defaultRounding: 'bank'
});
break;
}

Imagine different configurations for development and production environments:

// Different configurations for different environments
if (process.env.NODE_ENV === 'development') {
Amount.configure({
defaultCurrency: 'USD',
defaultRounding: 'math'
});
} else {
Amount.configure({
defaultCurrency: process.env.DEFAULT_CURRENCY || 'USD',
defaultRounding: 'bank' // More conservative for production
});
}

Project

Built with by Jo Santana in Brazil.

© 2026 Inpulse. All rights reserved.