Added algorithm to normalize double values from JavaScript to JSON-LD.
authorManu Sporny <msporny@digitalbazaar.com>
Tue, 01 Feb 2011 17:03:34 -0500
changeset 19 95d53b53c8cb
parent 18 0f25798ac02f
child 20 1f999400e01a
Added algorithm to normalize double values from JavaScript to JSON-LD.
spec/latest/index.html
--- a/spec/latest/index.html	Tue Feb 01 16:43:19 2011 -0500
+++ b/spec/latest/index.html	Tue Feb 01 17:03:34 2011 -0500
@@ -1754,11 +1754,26 @@
 
 <p>When normalizing <strong>xsd:double</strong> values, implementers MUST
 ensure that the normalized value is a string. In order to generate the
-string from a <strong>double</code> value, output equivalent to the
+string from a <strong>double</strong> value, output equivalent to the
 <code>printf("%1.6e", value)</code> function in C MUST be used where
 <strong>"%1.6e"</strong> is the string formatter and <strong>value</strong>
 is the value to be converted.</p>
 
+<p>To convert the a double value in JavaScript, implementers can use the 
+following snippet of code:</p>
+
+<pre class="example" data-transform="updateExample">
+<!--
+// the double value to convert to a JSON-LD normalized string
+var value = 2.33333333333;
+// Convert the value, producing the equivalent output to printf("%1.6e", value);
+var s = (value).toExponential(6); 
+s = s.substring(0, s.indexOf('e') + 2) + 
+   (s.indexOf('e') == s.length - 3 ? '0' : '') + 
+   s.substr(s.indexOf('e') + 2);
+-->
+</pre>
+
 <p class="note">When data needs to be normalized, JSON-LD authors should
 not use values that are going to undergo automatic conversion. This is due
 to the lossy nature of <strong>xsd:double</strong> values.</p>