Bug 1504181 - Force scientific notation in CanonicalJSON r=glasserc

Mimic python for small float numbers

Differential Revision: https://phabricator.services.mozilla.com/D10734

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Mathieu Leplatre 2018-11-12 14:12:58 +00:00
Родитель a8c4d8395e
Коммит a0fb02038c
1 изменённых файлов: 19 добавлений и 0 удалений

Просмотреть файл

@ -139,3 +139,22 @@ add_task(async function test_canonicalJSON_with_deeply_nested_dicts() {
Assert.equal(CanonicalJSON.stringify(records), expected);
});
add_task(async function test_canonicalJSON_should_use_scientific_notation() {
/*
We globally follow the Python float representation, except for exponents < 10
where we drop the leading zero
*/
Assert.equal(CanonicalJSON.stringify(.00099), "0.00099");
Assert.equal(CanonicalJSON.stringify(.000011), "0.000011");
Assert.equal(CanonicalJSON.stringify(.0000011), "0.0000011");
Assert.equal(CanonicalJSON.stringify(.000001), "0.000001");
Assert.equal(CanonicalJSON.stringify(.00000099), "9.9e-7");
Assert.equal(CanonicalJSON.stringify(.0000001), "1e-7");
Assert.equal(CanonicalJSON.stringify(.000000930258908), "9.30258908e-7");
Assert.equal(CanonicalJSON.stringify(.00000000000068272), "6.8272e-13");
Assert.equal(CanonicalJSON.stringify(Math.pow(10, 20)), "100000000000000000000");
Assert.equal(CanonicalJSON.stringify(Math.pow(10, 21)), "1e+21");
Assert.equal(CanonicalJSON.stringify(Math.pow(10, 15) + 0.1), "1000000000000000.1");
Assert.equal(CanonicalJSON.stringify(Math.pow(10, 16) * 1.1), "11000000000000000");
});