Currency and Number Formatting in AI Agent Responses
Implement locale-aware currency formatting, multi-currency conversion, and precise number display in AI agent responses for global user bases.
Why Number Formatting Matters for AI Agents
The number 1,234.56 in the United States is written as 1.234,56 in Germany and 1 234,56 in France. When an AI agent reports financial data, product prices, or analytics metrics, using the wrong format is confusing at best and dangerous at worst — a misplaced decimal separator could turn a $1,234 invoice into $1.234 (just over one dollar).
AI agents that handle any numeric output must be locale-aware. This is not about cosmetics; it is about correctness.
Locale-Aware Number Formatting
Python's babel library provides comprehensive locale formatting. Build a formatter class that handles numbers, currencies, and percentages.
from babel.numbers import (
format_decimal,
format_currency,
format_percent,
format_compact_decimal,
)
from dataclasses import dataclass
@dataclass
class NumberFormatter:
locale: str = "en_US"
def decimal(self, value: float, decimal_places: int = 2) -> str:
return format_decimal(value, format=f"#,##0.{'0' * decimal_places}", locale=self.locale)
def currency(self, amount: float, currency_code: str = "USD") -> str:
return format_currency(amount, currency_code, locale=self.locale)
def percent(self, value: float) -> str:
return format_percent(value, format="#,##0.0%", locale=self.locale)
def compact(self, value: float) -> str:
"""Format large numbers compactly: 1.2M, 450K, etc."""
return format_compact_decimal(value, locale=self.locale)
# Examples
us = NumberFormatter("en_US")
de = NumberFormatter("de_DE")
ja = NumberFormatter("ja_JP")
print(us.currency(1234.56)) # $1,234.56
print(de.currency(1234.56, "EUR")) # 1.234,56 EUR (with locale symbol)
print(ja.currency(1234.56, "JPY")) # JPY 1,235 (no decimals for yen)
Multi-Currency Conversion
When users ask about prices or costs in their local currency, the agent needs real-time (or cached) exchange rates.
import httpx
from datetime import datetime, timedelta
from typing import Dict, Optional
class CurrencyConverter:
def __init__(self, cache_ttl_minutes: int = 60):
self._rates: Dict[str, float] = {}
self._base_currency: str = "USD"
self._last_updated: Optional[datetime] = None
self._cache_ttl = timedelta(minutes=cache_ttl_minutes)
async def _refresh_rates(self) -> None:
now = datetime.utcnow()
if self._last_updated and (now - self._last_updated) < self._cache_ttl:
return
async with httpx.AsyncClient() as client:
resp = await client.get(
"https://api.exchangerate-api.com/v4/latest/USD"
)
data = resp.json()
self._rates = data["rates"]
self._base_currency = data["base"]
self._last_updated = now
async def convert(self, amount: float, from_cur: str, to_cur: str) -> float:
await self._refresh_rates()
if from_cur == to_cur:
return amount
# Convert to base (USD) then to target
in_base = amount / self._rates.get(from_cur, 1.0)
return in_base * self._rates.get(to_cur, 1.0)
async def format_converted(
self, amount: float, from_cur: str, to_cur: str, locale: str = "en_US"
) -> str:
converted = await self.convert(amount, from_cur, to_cur)
formatter = NumberFormatter(locale)
return formatter.currency(converted, to_cur)
Precision Rules Per Currency
Different currencies have different decimal precision rules. Japanese yen and Korean won use zero decimal places. Kuwaiti dinar uses three. Your formatting must respect these conventions.
See AI Voice Agents Handle Real Calls
Book a free demo or calculate how much you can save with AI voice automation.
CURRENCY_PRECISION = {
"USD": 2, "EUR": 2, "GBP": 2, "JPY": 0, "KRW": 0,
"BHD": 3, "KWD": 3, "OMR": 3, "INR": 2, "CNY": 2,
"BRL": 2, "MXN": 2, "CHF": 2, "AUD": 2, "CAD": 2,
}
def round_for_currency(amount: float, currency_code: str) -> float:
"""Round amount to the correct precision for the currency."""
precision = CURRENCY_PRECISION.get(currency_code, 2)
return round(amount, precision)
class PrecisionAwareFormatter:
def __init__(self, locale: str = "en_US"):
self.locale = locale
def format(self, amount: float, currency_code: str) -> str:
rounded = round_for_currency(amount, currency_code)
return format_currency(rounded, currency_code, locale=self.locale)
Integrating Into Agent Responses
Build a response processor that detects numeric values in agent output and reformats them for the user's locale.
import re
class NumericResponseProcessor:
def __init__(self, formatter: NumberFormatter):
self.formatter = formatter
def process_response(self, response: str, user_currency: str = "USD") -> str:
"""Find and reformat currency amounts in agent responses."""
# Match patterns like $1,234.56 or USD 1234.56
currency_pattern = r"\$([\d,]+\.?\d*)"
def replace_usd(match):
raw = match.group(1).replace(",", "")
try:
val = float(raw)
return self.formatter.currency(val, user_currency)
except ValueError:
return match.group(0)
return re.sub(currency_pattern, replace_usd, response)
# Usage
processor = NumericResponseProcessor(NumberFormatter("de_DE"))
raw_response = "The total cost is $1,234.56 per month."
localized = processor.process_response(raw_response, "EUR")
# Output uses German formatting with Euro symbol
Handling Ambiguous Number Formats in User Input
When users type numbers, they may use their locale's conventions. The agent must parse "1.234,56" (German) as 1234.56, not as a date or invalid number.
from babel.numbers import parse_decimal
def parse_user_number(text: str, locale: str = "en_US") -> float:
"""Parse a number from user input respecting their locale."""
try:
return float(parse_decimal(text, locale=locale))
except Exception:
# Fallback: strip non-numeric chars except . and -
cleaned = re.sub(r"[^\d.\-]", "", text)
return float(cleaned) if cleaned else 0.0
FAQ
How do I decide which currency to display by default?
Use the user's locale to infer their likely currency (e.g., de_DE maps to EUR, ja_JP maps to JPY). Allow users to override this in their profile settings. For e-commerce agents, always display the product's base currency alongside the user's local currency so there is no ambiguity.
Should I show exchange rates in agent responses?
Yes, when performing conversions. Show both the original amount and the converted amount with a note like "approximately" to signal that the rate may fluctuate. Include the rate source and timestamp for financial applications.
How do I handle cryptocurrency amounts?
Cryptocurrencies typically use 8 decimal places (BTC) or 18 (ETH for gas). Use a custom precision map for crypto and display in scientific notation for very small amounts. Always specify the asset symbol explicitly since there is no locale convention for crypto formatting.
#CurrencyFormatting #NumberLocalization #Internationalization #AIAgents #FinancialData #AgenticAI #LearnAI #AIEngineering
CallSphere Team
Expert insights on AI voice agents and customer communication automation.
Try CallSphere AI Voice Agents
See how AI voice agents work for your industry. Live demo available -- no signup required.