Skip to content

Commit

Permalink
Add permille units with ‰ symbol (#2033)
Browse files Browse the repository at this point in the history
  • Loading branch information
Musaefendic authored Jul 8, 2024
1 parent 7035daf commit 74e2e6a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
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

0 comments on commit 74e2e6a

Please sign in to comment.