Skip to main content

No project description provided

Project description

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 number

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

Percent 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

Percent supports mathematical operations

assert Percent(10) + Percent(10) == Percent(20)
assert Percent(10) + 10 == Percent(20)

assert Percent(20) - Percent(10) == Percent(10)
assert Percent(20) - 10 == Percent(10)

assert Percent(20) * 2 == Percent(40)
assert Percent(20) / 2 == Percent(10)
assert Percent(20) // 3 == Percent(6)
assert Percent(20) % 3 == Percent(2)

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

percents-1.1.1.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

percents-1.1.1-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file percents-1.1.1.tar.gz.

File metadata

  • Download URL: percents-1.1.1.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.12

File hashes

Hashes for percents-1.1.1.tar.gz
Algorithm Hash digest
SHA256 782d934a035210541fe6cba5a758c9bfaaa817dfc9c3686cd98d9ae3ee0d97e2
MD5 30d416a21a78f771303bc87d6b7d8d80
BLAKE2b-256 9a1667eb36d36e96f04d94d35375ba13642255729c05994aa3c8550877d1917d

See more details on using hashes here.

File details

Details for the file percents-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: percents-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.12

File hashes

Hashes for percents-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c67f600130a4a665085824f4a8877015081306dcf12a71c51e06df60ae1bbf97
MD5 b6643a6149b4050d7bb363a60aa84bca
BLAKE2b-256 3e402c1d377dad1f92ee4488ae175611c44d5089be17a00372a2b0f7f8b13b2e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page