🛠️ToolsShed

Formatador de Números

Formate números como moeda, porcentagem, notação científica ou com separadores personalizados.

Perguntas Frequentes

Implementação de Código

# Built-in f-string and format() number formatting

n = 1234567.891

# Thousands separator + 2 decimal places
print(f"{n:,.2f}")        # 1,234,567.89
print(format(n, ',.2f'))  # 1,234,567.89

# No decimal places (integer-style)
print(f"{n:,.0f}")        # 1,234,568

# Scientific notation
print(f"{n:.3e}")         # 1.235e+06

# Percentage
ratio = 0.1234
print(f"{ratio:.1%}")     # 12.3%

# Left/right pad with width
print(f"{n:>15,.2f}")     #  1,234,567.89

# Locale-aware formatting with the babel library
# pip install babel
from babel.numbers import format_number, format_currency

print(format_number(n, locale='de_DE'))      # 1.234.567,891
print(format_number(n, locale='fr_FR'))      # 1 234 567,891
print(format_currency(1234.5, 'USD', locale='en_US'))  # $1,234.50
print(format_currency(1234.5, 'EUR', locale='de_DE'))  # 1.234,50 €
print(format_currency(1500, 'JPY', locale='ja_JP'))    # ¥1,500

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.