Pemformat Angka
Format angka sebagai mata uang, persentase, notasi ilmiah, atau dengan pemisah kustom.
Pertanyaan yang Sering Diajukan
Implementasi Kode
# 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,500Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.