Skip to content

nstonic/percents

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Percents

Official public repository

Calculating percentages, subtracting a discount, adding a markup is a very common task. The “Percents” library simplifies the work with percentages. To use it, all you need to do is import the Percent type

from percents import Percent

Next, the idea is simple. For example, we have a sum of $1000, from which we need to subtract 38%.

1000 - Percent(38)
>>> 620

In the same way we can add

1000 + Percent(38)
>>> 1380

To take 38% from this amount, all you have to do is multiply the following

1000 * Percent(38)
>>> 380

The type of data returned always matches the type of the original one

result = 1000.0 + Percent(38)
>>> 1380
type(result)
>>> float
result = Decimal(1000) + Percent(38)
>>> 1380
type(result)
>>> Decimal

Even for strings

result = '1000' + Percent(38)
>>> '1380'
type(result)
>>> str

Percents can be compared to each other

percent1 = Percent(10)
percent2 = Percent(20)
assert percent2 != percent1
assert percent2 > percent1
assert percent1 < percent2

percent1 = Percent(10)
percent2 = Percent(10)
assert percent2 == percent1
assert percent2 >= percent1
assert percent1 <= percent2

and with other numbers. In this case, the comparison is in terms of percentage points.

percent = Percent(10)
assert percent == 10
assert percent != 11
assert percent > 9
assert percent < 11
assert percent >= 10.0
assert percent <= 10.0

Attributes

The Percent type has two attributes

  • Percent.value -- value in percentage points
  • Percent.multiplier -- multiplier
percent = Percent(10)
percent.value
>>> 10
percent.multiplier
>>> 0.1

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages