Skip to content

Amount

getCents

1.0.0

Gets the cents value of an amount, which represents the internal integer representation used for precise calculations.

amount.getCents();
  • Returns the internal cents representation as a number
  • Provides access to the precise integer value used internally
  • Useful for low-level operations and debugging
  • Currency-agnostic - returns raw cents value
  • Simple and intuitive API
  • No side effects - read-only operation
  • Comprehensive testing
ArgTypeDefault ValueRequired
No arguments required

From an Amount instance, use the getCents() method to get the internal cents value.

import { amount } from '@inpulse-ui/utils';
const myAmount = amount(123.45, 'USD');
const cents = myAmount.getCents();
console.log(cents); // 12345

Works with different currencies:

const usdAmount = amount(99.99, 'USD');
const eurAmount = amount(50.75, 'EUR');
console.log(usdAmount.getCents()); // 9999
console.log(eurAmount.getCents()); // 5075

Handles negative values:

const negativeAmount = amount(-25.50, 'GBP');
const cents = negativeAmount.getCents();
console.log(cents); // -2550

Zero values:

const zeroAmount = amount(0, 'CAD');
const cents = zeroAmount.getCents();
console.log(cents); // 0

Useful for precise comparisons:

const amount1 = amount(10.50, 'USD');
const amount2 = amount(10.51, 'USD');
console.log(amount1.getCents()); // 1050
console.log(amount2.getCents()); // 1051
// Direct cents comparison
if (amount1.getCents() < amount2.getCents()) {
console.log('Amount1 is smaller');
}

You can use getCents() for Google Tag Manager Data Layer when tracking e-commerce events. Many analytics platforms and pixel tags require monetary values in cents for precise tracking.

import { amount } from '@inpulse-ui/utils';
// E-commerce purchase tracking
function trackPurchase(orderValue, currency, items) {
const totalAmount = amount(orderValue, currency);
// Push to GTM Data Layer with cents value
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'purchase',
ecommerce: {
transaction_id: 'ORDER_123456',
value: totalAmount.getCents(), // 4995 for $49.95
currency: currency,
items: items.map(item => ({
item_id: item.id,
item_name: item.name,
price: amount(item.price, currency).getCents(), // Convert each item to cents
quantity: item.quantity
}))
}
});
}
// Usage
const orderItems = [
{ id: 'SKU001', name: 'Product A', price: 29.99, quantity: 1 },
{ id: 'SKU002', name: 'Product B', price: 19.96, quantity: 1 }
];
trackPurchase(49.95, 'USD', orderItems);
// Results in Data Layer:
// {
// event: 'purchase',
// ecommerce: {
// transaction_id: 'ORDER_123456',
// value: 4995, // $49.95 in cents
// currency: 'USD',
// items: [
// { item_id: 'SKU001', item_name: 'Product A', price: 2999, quantity: 1 },
// { item_id: 'SKU002', item_name: 'Product B', price: 1996, quantity: 1 }
// ]
// }
// }

On a Facebook Pixel integration, you might also need to send the value in cents for better accuracy:

import { amount } from '@inpulse-ui/utils';
// Track Facebook Pixel purchase event
function trackFacebookPurchase(orderValue, currency) {
const totalAmount = amount(orderValue, currency);
// Facebook Pixel expects value in cents for some currencies
fbq('track', 'Purchase', {
value: totalAmount.getCents() / 100, // Convert back to decimal for FB
currency: currency,
// Alternative: send raw cents if your setup requires it
value_cents: totalAmount.getCents() // 12345 for $123.45
});
}
// Usage
trackFacebookPurchase(123.45, 'USD');

Now imagine its usage in payment gateway integrations:

import { amount } from '@inpulse-ui/utils';
// Stripe payment integration with precise decimal handling
async function createStripePayment(orderTotal, currency) {
const paymentAmount = amount(orderTotal, currency);
// Stripe expects amounts in smallest currency unit (cents for USD)
// But for display and validation, we use decimal values
const stripePayment = {
amount: paymentAmount.getCents(), // 12999 (cents for Stripe API)
currency: currency.toLowerCase(),
metadata: {
display_amount: paymentAmount.getValue(), // 129.99 (for records)
formatted_amount: paymentAmount.format() // "$129.99" (for receipts)
},
description: `Order total: ${paymentAmount.format()}`
};
try {
const paymentIntent = await stripe.paymentIntents.create(stripePayment);
// Log for analytics with decimal value
console.log(`Payment created: ${paymentAmount.getValue()} ${currency}`);
return paymentIntent;
} catch (error) {
console.error('Payment failed for amount:', paymentAmount.getValue());
throw error;
}
}
// Usage
createStripePayment(129.99, 'USD');

Project

Built with by Jo Santana in Brazil.

© 2026 Inpulse. All rights reserved.