absfuyu.util.shorten_number module
Absfuyu: Shorten number
Short number base on suffixes
Version: 5.1.0 Date updated: 10/03/2025 (dd/mm/yyyy)
- class absfuyu.util.shorten_number.UnitSuffixFactory(base, short_name, full_name)[source]
Bases:
NamedTuple
Added in version 4.1.0
- base: int
Alias for field number 0
- short_name: list[str]
Alias for field number 1
- full_name: list[str]
Alias for field number 2
- class absfuyu.util.shorten_number.CommonUnitSuffixesFactory[source]
Bases:
object
Added in version 4.1.0
- NUMBER = (1000, ['', 'K', 'M', 'B', 'T', 'Qa', 'Qi', 'Sx', 'Sp', 'Oc', 'No', 'Dc', 'Ud', 'Dd', 'Td', 'Qad', 'Qid', 'Sxd', 'Spd', 'Ocd', 'Nod', 'Vg', 'Uvg', 'Dvg', 'Tvg', 'Qavg', 'Qivg', 'Sxvg', 'Spvg', 'Ovg', 'Nvg', 'Tg', 'Utg', 'Dtg', 'Ttg', 'Qatg', 'Qitg', 'Sxtg', 'Sptg', 'Otg', 'Ntg'], ['', 'Thousand', 'Million', 'Billion', 'Trillion', 'Quadrillion', 'Quintillion', 'Sextillion', 'Septillion', 'Octillion', 'Nonillion', 'Decillion', 'Undecillion', 'Duodecillion', 'Tredecillion', 'Quattuordecillion', 'Quindecillion', 'Sexdecillion', 'Septendecillion', 'Octodecillion', 'Novemdecillion', 'Vigintillion', 'Unvigintillion', 'Duovigintillion', 'Tresvigintillion', 'Quattuorvigintillion', 'Quinvigintillion', 'Sesvigintillion', 'Septemvigintillion', 'Octovigintillion', 'Novemvigintillion', 'Trigintillion', 'Untrigintillion', 'Duotrigintillion', 'Trestrigintillion', 'Quattuortrigintillion', 'Quintrigintillion', 'Sestrigintillion', 'Septentrigintillion', 'Octotrigintillion', 'Noventrigintillion'])
- DATA_SIZE = (1024, ['b', 'Kb', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'BB'], ['byte', 'kilobyte', 'megabyte', 'gigabyte', 'terabyte', 'petabyte', 'exabyte', 'zetabyte', 'yottabyte', 'brontobyte'])
- class absfuyu.util.shorten_number.Decimal(original_value: int | float, base: ~typing.Annotated[int, 'positive', 'not_zero'] = 1000, suffixes: list[str] = <factory>, factory: ~absfuyu.util.shorten_number.UnitSuffixFactory | None = None, suffix_full_name: bool = False)[source]
Bases:
object
Shorten large number
- Parameters:
original_value (int | float) – Value to shorten
base (int) – Short by base (must be > 0)
suffixes (list[str]) – List of suffixes to use (ascending order)
factory (UnitSuffixFactory | None) –
UnitSuffixFactory
to use (will overwritebase
andsuffixes
)suffix_full_name (bool) – Use suffix full name (available with
UnitSuffixFactory
), by defaultFalse
- Returns:
Decimal instance
- Return type:
Added in version 4.1.0
- original_value: int | float
- base: Annotated[int, 'positive', 'not_zero'] = 1000
- suffixes: list[str]
- factory: UnitSuffixFactory | None = None
- suffix_full_name: bool = False
- value: int | float
- suffix: str
- classmethod number(value: int | float, suffix_full_name: bool = False) Self [source]
Decimal for normal large number
- classmethod data_size(value: int | float, suffix_full_name: bool = False) Self [source]
Decimal for data size
- to_text(decimal: int = 2, *, separator: str = ' ', float_only: bool = True) str [source]
Convert to string
- Parameters:
decimal (int, optional) – Round up to which decimal, by default
2
separator (str, optional) – Character between value and suffix, by default
" "
float_only (bool, optional) – Returns value as <float> instead of <int> when
decimal = 0
, by defaultTrue
- Returns:
Decimal string
- Return type:
str
- absfuyu.util.shorten_number.shorten_number(f: Callable[[P], N]) Callable[[P], Decimal] [source]
Shorten the number value by name
- Parameters:
f (Callable[P, N]) – Function that return
int
orfloat
- Returns:
Function that return
Decimal
- Return type:
Callable[P, Decimal]
Usage
Use this as a decorator (
@shorten_number
)Example:
>>> import random >>> @shorten_number >>> def big_num() -> int: ... random.randint(100000000, 10000000000) >>> big_num() 4.20 B
Added in version 5.0.0