How To Reduce Numbers' Significance In Json's Stringify
Solution 1:
Native JSON.stringify accepts the parameter replacer
, which can be a function converting values to whatever is needed:
a = [0.123456789123456789]
JSON.stringify(a, function(key, val) {
returnval.toFixed ? Number(val.toFixed(3)) : val;
})
>> "[0.123]"
Solution 2:
Use Number#toPrecision
or Number#toFixed
, whichever suits your needs better. The difference is that toFixed
sets the number of digits after the decimal point, and toPrecision
sets the entire number of digits.
var foo = 1.98765432;
console.log(foo.toPrecision(5)); // 1.9877
Note that toFixed
and toPrecision
return strings, so you'll probably want to convert them back to numbers before JSONifying them.
Here's the obligatory MDN link.
You can also do something like this, if you want to roll your own.
Math.round(1.98765432 * 10000) / 10000// 1.9877
Encapsulated in a function, it might look something like this:
functionprecisify(n, places) {
var scale = Math.pow(10, places);
returnMath.round(n * scale) / scale;
}
Solution 3:
The first way that comes to mind is to pre-process the array to round all the numbers to four digits:
vararray =[{"x":-0.825,"y":0},
{"x":-1.5812500000000003,"y":-0.5625},
{"x":-2.2515625000000004,"y":-1.546875}];
for (var i=0; i<array.length; i++){
array[i].x = +array[i].x.toFixed(4);
array[i].y = +array[i].y.toFixed(4);
}
var json = JSON.stringify(array);
// json === '[{"x":-0.825,"y":0},{"x":-1.5813,"y":-0.5625},{"x":-2.2516,"y":-1.5469}]'
The .toFixed()
function returns a string, so I've used the unary plus operator to convert that back to a number.
The second way that comes to mind is to process the string output by JSON.stringify()
:
json = json.replace(/(\.\d{4})(\d+)/g,"$1");
Solution 4:
A bit of overkill, but this should work in all situations.
functionprepareArrayForJSON(array) {
var i = 0;
while (i < array.length) {
if (typeof array[i] === 'number') {
array[i] = array[i].toPrecision(4);
} elseif(typeof array[i] === 'object') {
if (arrayinstanceofObject) {
prepareObjectForJSON(array[i]);
} elseif (arrayinstanceofArray) {
prepareArrayForJSON(array[i]);
}
}
}
}
functionprepareObjectForJSON(obj) {
var i;
for (i in obj) {
if (obj.hasOwnProperty(i)) {
if (typeof obj[i] === 'number') {
obj[i] = obj[i].toPrecision(4);
} elseif(typeof obj[i] === 'object') {
if (obj instanceofObject) {
prepareObjectForJSON(array[i]);
} elseif (obj instanceofArray) {
prepareArrayForJSON(obj[i]);
}
}
}
}
}
Enjoy yourself
Solution 5:
Run over your array and apply the following to all values.
value = Math.round( value * 10000 ) / 10000;
Another possibility is to store your coordinates internally as int values (multiples of 10^-4) and just convert them for output.
Post a Comment for "How To Reduce Numbers' Significance In Json's Stringify"