diff --git a/CHANGELOG.md b/CHANGELOG.md index 05de96ca..3b69c4d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - Inline contexts that try to redefine @context now raise an error. Fixes [expand#ter56](https://w3c.github.io/json-ld-api/tests/expand-manifest.html#ter56) and [toRdf#ter56](https://w3c.github.io/json-ld-api/tests/toRdf-manifest.html#ter56). - When using native types, values of `xsd:boolean` and `xsd:integer` are now properly converted. Fixes [fromRdf#t0027](https://w3c.github.io/json-ld-api/tests/fromRdf-manifest.html#t0027). - Numbers with 0 as fractional part now parse to an `xsd:integer` instead of `xsd:double`. Fixes [toRdf#ttn02](https://w3c.github.io/json-ld-api/tests/toRdf-manifest.html#ttn02). +- Zeros are now truncated for numeric values with negative exponent. ## 3.1.0 - 2026-06-19 diff --git a/lib/pyld/jsonld.py b/lib/pyld/jsonld.py index 529280e0..a18f32e8 100644 --- a/lib/pyld/jsonld.py +++ b/lib/pyld/jsonld.py @@ -6388,7 +6388,7 @@ def _is_double(v): def _canonicalize_double(value: float) -> str: """Convert a float value to canonical lexical form of `xsd:double`.""" - return re.sub(r'(\d)0*E\+?0*(\d)', r'\1E\2', (f'{value:1.15E}')) + return re.sub(r'(\d)0*E\+?(-)?0*(\d)', r'\1E\2\3', (f'{value:1.15E}')) def _is_numeric(v): diff --git a/tests/test_jsonld.py b/tests/test_jsonld.py index 9a753df8..d9efdc81 100644 --- a/tests/test_jsonld.py +++ b/tests/test_jsonld.py @@ -1053,6 +1053,18 @@ def test_fractional(self): nquads = jsonld.to_rdf(input, options={'format': 'application/n-quads'}) assert nquads == expected + # Issue 175 + def test_truncate_zeros_with_negative_exponent_numbers(self): + """ + Numeric values with negative exponent should truncate zeros + """ + input = { "ex:value": 0.97 } + + expected = '_:b0 "9.7E-1"^^ .\n' + + nquads = jsonld.to_rdf(input, options={'format': 'application/n-quads'}) + assert nquads == expected + class TestFromRDF: def test_compound_literal_direction_without_language(self): """