Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add permille units with ‰ symbol #2033

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Pint Changelog
0.25 (unreleased)
-----------------

- Nothing added yet.
- Support permille units and `‰` symbol (PR #2033, Issue #1963)


0.24.1 (2024-06-24)
Expand Down
1 change: 1 addition & 0 deletions pint/default_en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ byte = 8 * bit = B = octet

# Ratios
percent = 0.01 = %
permille = 0.001 = ‰
ppm = 1e-6

# Length
Expand Down
3 changes: 3 additions & 0 deletions pint/facets/plain/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ def __init__(
# use a default preprocessor to support "%"
self.preprocessors.insert(0, lambda string: string.replace("%", " percent "))

# use a default preprocessor to support permille "‰"
self.preprocessors.insert(0, lambda string: string.replace("‰", " permille "))

#: mode used to fill in the format defaults
self.separate_format_defaults = separate_format_defaults

Expand Down
18 changes: 18 additions & 0 deletions pint/testsuite/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,24 @@ def test_issue1277(self, module_registry):
assert c.to("percent").m == 50
# assert c.to("%").m == 50 # TODO: fails.

def test_issue1963(self, module_registry):
ureg = module_registry
assert ureg("‰") == ureg("permille")
assert ureg("‰") == ureg.permille

a = ureg.Quantity("10 ‰")
b = ureg.Quantity("100 ppm")
c = ureg.Quantity("0.5")

assert f"{a}" == "10 permille"
assert f"{a:~}" == "10 ‰"

assert_equal(a, 0.01)
assert_equal(1e2 * b, a)
assert_equal(c, 50 * a)

assert_equal((1 * ureg.milligram) / (1 * ureg.gram), ureg.permille)

@pytest.mark.xfail
@helpers.requires_uncertainties()
def test_issue_1300(self):
Expand Down
Loading