Repository: RDFLib/rdflib

Issue:
Incorrect turtle serialization of decimal values
I am using rdflib to serialize to ttl files, rdflib = 5.0.0

Here is a sample output,
```
wd:Q29722949 a wikibase:Item ;
    p:P2020013 wds:Q29722949-9ef7c6be-2c5e-4c64-8e44-a3af6725596e ;
    p:P2020014 wds:Q29722949-5570794f-5918-45b8-936f-9a2e24a6a61d ;
    p:P2020015 wds:Q29722949-62e5de90-74ab-47bd-a8b5-13323986c20b ;
    p:P2020016 wds:Q29722949-daa7b2f2-3646-4e78-bdb3-eb01c745d502 ;
    p:P2020017 wds:Q29722949-0e7cb2f1-a13b-47a0-b6a9-c24908c0edcf ;
    wdtn:P2020013 wdv:Quantityc1c0c0c0 ;
    wdtn:P2020014 wdv:Quantityc0c0c0c0 ;
    wdtn:P2020015 wdv:Quantityc0-000000216c0c0c0 ;
    wdtn:P2020016 wdv:Quantityc0c0c0c0 ;
    wdtn:P2020017 wdv:Quantityc0-0000000000000005c0c0c0 ;
    wdt:P2020013 1.0 ;
    wdt:P2020014 0.0 ;
    wdt:P2020015 2.16E-7 ;
    wdt:P2020016 0.0 ;
    wdt:P2020017 5E-16.0 .
```

notice that the line `wdt:P2020015 2.16E-7` is correct while, ` wdt:P2020017 5E-16.0 ` is incorrect as it has a `.0` at the end, which is invalid.

Please take a look, this seems to be happening randomly

Discussion:

Patch:
diff --git a/rdflib/term.py b/rdflib/term.py
index 32ec6af4..ac69c3bd 100644
--- a/rdflib/term.py
+++ b/rdflib/term.py
@@ -1259,10 +1259,9 @@ class Literal(Identifier):
                     return sub("\\.?0*e", "e", "%e" % float(self))
                 elif self.datatype == _XSD_DECIMAL:
                     s = "%s" % self
-                    if "." not in s:
+                    if "." not in s and "e" not in s and "E" not in s:
                         s += ".0"
                     return s
-
                 elif self.datatype == _XSD_BOOLEAN:
                     return ("%s" % self).lower()
                 else:


Test patch:
diff --git a/test/test_issue1043.py b/test/test_issue1043.py
new file mode 100644
index 00000000..db202d77
--- /dev/null
+++ b/test/test_issue1043.py
@@ -0,0 +1,33 @@
+import decimal
+import unittest
+import io
+import sys
+
+from rdflib import Graph, Namespace, XSD, RDFS, Literal
+
+
+class TestIssue1043(unittest.TestCase):
+
+    def test_issue_1043(self):
+        expected = """@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
+@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
+
+<http://example.org/number> rdfs:label 4e-08 .
+
+
+"""
+        capturedOutput = io.StringIO()
+        sys.stdout = capturedOutput
+        g = Graph()
+        g.bind('xsd', XSD)
+        g.bind('rdfs', RDFS)
+        n = Namespace("http://example.org/")
+        g.add((n.number, RDFS.label, Literal(0.00000004, datatype=XSD.decimal)))
+        print(g.serialize(format="turtle").decode("utf-8"))
+        sys.stdout = sys.__stdout__
+        self.assertEqual(capturedOutput.getvalue(), expected)
+
+
+
+if __name__ == "__main__":
+    unittest.main()
\ No newline at end of file