Amount
getCents
1.0.0- Dependencies:
- ∟Direct: 0
- ∟Peer: 0
- Source code ↗
- Check on NPM ↗
Hidden
Overview
Section titled “Overview”Gets the cents value of an amount, which represents the internal integer representation used for precise calculations.
amount.getCents();Features
Section titled “Features”- 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
Arguments
Section titled “Arguments”| Arg | Type | Default Value | Required |
| 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); // 12345Works with different currencies:
const usdAmount = amount(99.99, 'USD');const eurAmount = amount(50.75, 'EUR');
console.log(usdAmount.getCents()); // 9999console.log(eurAmount.getCents()); // 5075Handles negative values:
const negativeAmount = amount(-25.50, 'GBP');const cents = negativeAmount.getCents();
console.log(cents); // -2550Zero values:
const zeroAmount = amount(0, 'CAD');const cents = zeroAmount.getCents();
console.log(cents); // 0Useful for precise comparisons:
const amount1 = amount(10.50, 'USD');const amount2 = amount(10.51, 'USD');
console.log(amount1.getCents()); // 1050console.log(amount2.getCents()); // 1051
// Direct cents comparisonif (amount1.getCents() < amount2.getCents()) { console.log('Amount1 is smaller');}Examples
Section titled “Examples”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 trackingfunction 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 })) } });}
// Usageconst 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 eventfunction 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 });}
// UsagetrackFacebookPurchase(123.45, 'USD');Now imagine its usage in payment gateway integrations:
import { amount } from '@inpulse-ui/utils';
// Stripe payment integration with precise decimal handlingasync 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; }}
// UsagecreateStripePayment(129.99, 'USD');Built with by Jo Santana in Brazil.
© 2026 Inpulse. All rights reserved.