Skip to content

Commit e46f66d

Browse files
authored
feat: Adds Format.compact_decimal utility
1 parent a30d7cf commit e46f66d

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

babel/support.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from babel.dates import format_date, format_datetime, format_time, \
1919
format_timedelta
2020
from babel.numbers import format_decimal, format_currency, \
21-
format_percent, format_scientific
21+
format_percent, format_scientific, format_compact_decimal
2222

2323

2424
class Format:
@@ -108,6 +108,17 @@ def decimal(self, number, format=None):
108108
"""
109109
return format_decimal(number, format, locale=self.locale)
110110

111+
def compact_decimal(self, number, format_type='short', fraction_digits=0):
112+
"""Return a number formatted in compact form for the locale.
113+
114+
>>> fmt = Format('en_US')
115+
>>> fmt.compact_decimal(123456789)
116+
u'123M'
117+
"""
118+
return format_compact_decimal(number, format_type=format_type,
119+
fraction_digits=fraction_digits,
120+
locale=self.locale)
121+
111122
def currency(self, number, currency):
112123
"""Return a number in the given currency formatted for the locale.
113124
"""

tests/test_support.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,11 @@ def test_format_decimal():
329329
assert fmt.decimal(1.2345) == '1.234'
330330

331331

332+
def test_format_compact_decimal():
333+
fmt = support.Format('en_US')
334+
assert fmt.compact_decimal(1234567, format_type='long', fraction_digits=2) == '1.23 million'
335+
336+
332337
def test_format_percent():
333338
fmt = support.Format('en_US')
334339
assert fmt.percent(0.34) == '34%'

0 commit comments

Comments
 (0)