Skip to content

Amount

getValue

1.0.0

Gets the decimal value of an amount, converting from the internal cents representation to a human-readable decimal number.

amount.getValue();
  • Returns the decimal representation as a number
  • Converts from internal cents to decimal value
  • Handles negative values correctly
  • Currency-agnostic - returns raw decimal value
  • Simple and intuitive API
  • No side effects - read-only operation
  • Comprehensive testing
ArgTypeDefault ValueRequired
No arguments required

From an Amount instance, use the getValue() method to get the decimal value.

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

Works with different currencies:

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

Handles negative values:

const negativeAmount = amount(-25.50, 'GBP');
const value = negativeAmount.getValue();
console.log(value); // -25.5

Zero values:

const zeroAmount = amount(0, 'CAD');
const value = zeroAmount.getValue();
console.log(value); // 0

Mathematical operations with decimal values:

const amount1 = amount(10.50, 'USD');
const amount2 = amount(5.25, 'USD');
const value1 = amount1.getValue();
const value2 = amount2.getValue();
console.log(value1 + value2); // 15.75 (but prefer using amount.add() for precision)

Display and formatting purposes:

const price = amount(299.99, 'USD');
const value = price.getValue();
// Custom formatting
console.log(`Price: $${value.toFixed(2)}`); // Price: $299.99
// Calculations for display
const taxRate = 0.08;
const tax = value * taxRate;
console.log(`Tax: $${tax.toFixed(2)}`); // Tax: $24.00

You can use getValue() for Google Tag Manager Data Layer when tracking e-commerce events. Most analytics platforms expect decimal values for monetary amounts.

import { amount } from '@inpulse-ui/utils';
// Enhanced E-commerce tracking with decimal values
function trackEnhancedEcommerce(orderValue, currency, items) {
const totalAmount = amount(orderValue, currency);
// Push to GTM Data Layer with decimal values
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'purchase',
ecommerce: {
transaction_id: 'ORDER_789456',
value: totalAmount.getValue(), // 149.95 as decimal
currency: currency,
shipping: amount(9.99, currency).getValue(), // 9.99
tax: amount(12.75, currency).getValue(), // 12.75
items: items.map(item => ({
item_id: item.id,
item_name: item.name,
item_category: item.category,
price: amount(item.price, currency).getValue(), // Decimal price
quantity: item.quantity
}))
}
});
}
// Usage
const orderItems = [
{
id: 'SKU001',
name: 'Premium Headphones',
category: 'Electronics',
price: 89.99,
quantity: 1
},
{
id: 'SKU002',
name: 'USB Cable',
category: 'Accessories',
price: 19.99,
quantity: 2
}
];
trackEnhancedEcommerce(149.95, 'USD', orderItems);
// Results in Data Layer:
// {
// event: 'purchase',
// ecommerce: {
// transaction_id: 'ORDER_789456',
// value: 149.95, // Decimal value for analytics
// currency: 'USD',
// shipping: 9.99,
// tax: 12.75,
// items: [
// { item_id: 'SKU001', price: 89.99, quantity: 1 },
// { item_id: 'SKU002', price: 19.99, quantity: 2 }
// ]
// }
// }

On a Google Analytics 4 revenue tracking integration, you might also need to send the decimal value:

import { amount } from '@inpulse-ui/utils';
// Track GA4 purchase event with proper decimal formatting
function trackGA4Purchase(orderData) {
const {
orderId,
totalValue,
currency,
items,
shippingCost = 0,
taxAmount = 0
} = orderData;
const orderAmount = amount(totalValue, currency);
const shipping = amount(shippingCost, currency);
const tax = amount(taxAmount, currency);
// GA4 expects decimal values
gtag('event', 'purchase', {
transaction_id: orderId,
value: orderAmount.getValue(), // 234.97
currency: currency,
shipping: shipping.getValue(), // 15.00
tax: tax.getValue(), // 18.75
items: items.map(item => ({
item_id: item.sku,
item_name: item.name,
item_category: item.category,
price: amount(item.price, currency).getValue(), // Decimal price
quantity: item.quantity
}))
});
}
// Usage
trackGA4Purchase({
orderId: 'ORD_20241009_001',
totalValue: 234.97,
currency: 'USD',
shippingCost: 15.00,
taxAmount: 18.75,
items: [
{ sku: 'LAPTOP001', name: 'Gaming Laptop', category: 'Computers', price: 199.99, quantity: 1 }
]
});

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.