Skip to content
Learn Agentic AI
Learn Agentic AI10 min read3 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 Strategy

AI Agent M&A Activity 2026: Aircall–Vogent, Meta–PlayAI, OpenAI's Six Deals

Q1 2026 saw a record acquisition wave: Aircall bought Vogent (May), Meta acquired Manus and PlayAI, OpenAI closed six deals. The voice AI consolidation phase has begun.

Agentic AI

LangGraph State-Machine Architecture: A Principal-Engineer Deep Dive (2026)

How LangGraph's StateGraph, channels, and reducers actually work — with a working multi-step agent, eval hooks at every node, and the patterns that survive production.

Agentic AI

LangGraph Checkpointers in Production: Durable, Resumable Agents with Eval Replay

Use LangGraph's checkpointer to make agents resumable across crashes and human-in-the-loop pauses, then replay any checkpoint into your eval pipeline.

Agentic AI

Multi-Agent Handoffs with the OpenAI Agents SDK: The Pattern That Actually Scales (2026)

Handoffs done right — when one agent should hand control to another, how to preserve context, and how to evaluate the handoff decision itself.

Agentic AI

Building Your First Agent with the OpenAI Agents SDK in 2026: A Hands-On Walkthrough

Step-by-step build of a working agent with the OpenAI Agents SDK — Agent class, tools, handoffs, tracing — plus an eval pipeline that catches regressions before merge.

Agentic AI

LangGraph Supervisor Pattern: Orchestrating Multi-Agent Teams in 2026

The supervisor pattern in LangGraph for coordinating specialist agents, with full code, an eval pipeline that scores routing accuracy, and the failure modes to watch for.