Skip to content
Currency and Number Formatting in AI Agent Responses
Learn Agentic AI10 min read7 views

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.

Hear it before you finish reading

Talk to a live CallSphere AI voice agent in your browser — 60 seconds, no signup.

Try Live Demo →
flowchart LR
    INPUT(["User intent"])
    PARSE["Parse plus<br/>classify"]
    PLAN["Plan and tool<br/>selection"]
    AGENT["Agent loop<br/>LLM plus tools"]
    GUARD{"Guardrails<br/>and policy"}
    EXEC["Execute and<br/>verify result"]
    OBS[("Trace and metrics")]
    OUT(["Outcome plus<br/>next action"])
    INPUT --> PARSE --> PLAN --> AGENT --> GUARD
    GUARD -->|Pass| EXEC --> OUT
    GUARD -->|Fail| AGENT
    AGENT --> OBS
    style AGENT fill:#4f46e5,stroke:#4338ca,color:#fff
    style GUARD fill:#f59e0b,stroke:#d97706,color:#1f2937
    style OBS fill:#ede9fe,stroke:#7c3aed,color:#1e1b4b
    style OUT fill:#059669,stroke:#047857,color:#fff
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.

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.

Still reading? Stop comparing — try CallSphere live.

CallSphere ships complete AI voice agents per industry — 14 tools for healthcare, 10 agents for real estate, 4 specialists for salons. See how it actually handles a call before you book a demo.

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

Share

Try CallSphere AI Voice Agents

See how AI voice agents work for your industry. Live demo available -- no signup required.

Related Articles You May Like

AI Agents

Personal AI Assistant: How to Pick One for Business in 2026

A founder's guide to the personal AI assistant market: best AI assistant apps, business-grade options, and how CallSphere's voice agent fits in.

AI Agents

Free AI Agents in 2026: When Free Wins and When It Costs You

A founder's guide to free AI agents, low-code AI agent builders, and how to know when you should pay for a real platform like CallSphere.

Agentic AI

Graphiti: How Temporal Knowledge Graphs Give AI Voice Agents Persistent Memory (2026 Guide)

Graphiti is the open-source temporal knowledge graph for AI agents in 2026. Learn how bi-temporal memory beats vector RAG for voice agents and long-running LLMs.

AI Agents

Chatbot App vs ChatGPT: What's the Difference, and Which Do I Need?

Chatbot app vs ChatGPT in 2026: a founder's clear take on the difference, when to use which, and how a real AI chatbot app development works.

HVAC

Building an HVAC After-Hours Emergency Escalation System: A Complete Engineering Guide

How we built a fault-tolerant HVAC emergency triage and tech-dispatch platform on Kubernetes — three-tier CQRS, 11 micro-agents on the OpenAI Agents SDK + LangGraph, NATS JetStream, DTMF/SMS/WebSocket acceptance, circuit breakers, and an evaluation pipeline that catches regressions before they wake a tech at 3 AM.

AI Engineering

Anthropic and Moody's Data Partnership: Why Grounding Matters in Finance

Anthropic and Moody's announced a data partnership in May 2026 that grounds Claude in audited financial reference data. Why grounding reduces hallucination and what it unlocks.