display.format_decimal_numbers

format_decimal_numbers(text, format_string=None)

Format all decimal numbers in a LaTeX string with specified precision.

Searches for decimal numbers in LaTeX strings and applies Python format specifications to control precision and display. Used internally by show_eqn() for cell-level formatting but available for custom LaTeX string manipulation.

NOTE: Only matches standard decimal notation (e.g., “1.234”, “-0.567”). Does not match scientific notation or integers without decimal points.

Parameters

Name Type Description Default
text str | None LaTeX string containing decimal numbers to format. If None, returns None unchanged. required
format_string str | None Python format specification for float formatting. Supports flexible notation: - “.3f” (shorthand, common usage) - “{:.3f}” (full format string) - “:0.3f” (with zero-padding) If None, returns text unchanged. Defaults to None. None

Returns

Name Type Description
str | None LaTeX string with all decimal numbers formatted according to
str | None format_string, or None if text is None.

Raises

Name Type Description
ValueError If format_string is invalid or cannot format floats.

Examples

from keecas.display import format_decimal_numbers

# Basic usage with shorthand notation
latex = r"\sigma_{Sd} = 1.23456 \text{ MPa}"
formatted = format_decimal_numbers(latex, ".2f")
print(formatted)
\sigma_{Sd} = 1.23 \text{ MPa}
# Multiple decimal numbers in one string
latex = r"F = 100.567 \text{ kN}, A_{load} = 20.123 \text{ cm}^2"
formatted = format_decimal_numbers(latex, ".1f")
print(formatted)
F = 100.6 \text{ kN}, A_{load} = 20.1 \text{ cm}^2
# Different format specifications
latex = r"\alpha_{max} = 3.14159"

# Standard precision
print(format_decimal_numbers(latex, ".3f"))

# Scientific notation
print(format_decimal_numbers(latex, ".2e"))

# Zero-padding
print(format_decimal_numbers(latex, "06.2f"))
\alpha_{max} = 3.142
\alpha_{max} = 3.14e+00
\alpha_{max} = 003.14
# Integration with show_eqn workflow
from keecas import symbols, u, show_eqn
from sympy import latex

# tip: use for custom post-processing of LaTeX strings
sigma_Rd = symbols(r"\sigma_{Rd}")
value_latex = latex(5.123456 * u.MPa)

# Format specific parts before display
formatted_latex = format_decimal_numbers(value_latex, ".2f")
print(formatted_latex)
\mathtt{\text{5.12 MPa}}

See Also

  • show_eqn: Main display function with built-in float formatting
  • DisplayConfig: Display configuration (see default_float_format attribute)

Notes

  • Only matches decimal numbers (requires decimal point)
  • Negative numbers supported (matches leading minus sign)
  • Format string automatically normalized from shorthand to full format
  • Returns None unchanged if either text or format_string is None
  • Used internally by show_eqn() for per-cell formatting
  • Regex pattern: r”-?.” (matches standard decimal notation)