UniCoreFW

number_format()

Format a number with grouped thousands and fixed decimals.

Implementation

Args: value: The number to format precision: Number of decimal places decimal_sep: Character for the decimal point thousands_sep: Character for the thousands grouping Returns: The formatted number string, or "" if invalid

Example

number_format(1234567.89)

Expected output: "1,234,567.89"

Source Code

def number_format( value: Any, precision: int = 0, decimal_sep: str = ".", thousands_sep: str = "," ) -> str: if not isinstance(value, (int, float)) or not math.isfinite(value): return "" # Round to the given precision fmt = f"{{:,.{precision}f}}" result = fmt.format(value) # Python always uses ',' for thousands and '.' for decimal if thousands_sep != ",": result = result.replace(",", "TMP") if decimal_sep != ".": result = result.replace(".", decimal_sep) if thousands_sep != ",": result = result.replace("TMP", thousands_sep) return result