Open tobiasschweizer opened 4 years ago
Had the same problem with JS in-built round method. Found this workaround (see link in the JSDOC) which could be helpful for your case, too?
/**
* Helper method: round.
*
* It rounds a given number to a given number of decimal places.
* JS in-built round-method is sometimes not correct,
* see: {@link http://www.jacklmoore.com/notes/rounding-in-javascript/}.
*
* @param {number} value The given input value to be rounded.
* @param {number} decimals The given number of decimal places to round to.
* @returns {number} The rounded number.
*/
round(value: number, decimals: number): number {
if (Number.isNaN(value)) {
return;
}
return Number(Math.round(Number(value + 'e' + decimals)) + 'e-' + decimals);
}
@musicEnfanthen I will look at that, thanks!
Think about using a lib like https://www.npmjs.com/package/js-big-decimal to handle precision in decimal numbers so we do not loose information.
see https://github.com/dasch-swiss/knora-api-js-lib/pull/115#issuecomment-564600574
remove https://github.com/dasch-swiss/knora-api-js-lib/blob/8eafba8796e536449253af1e12019bdde92bd1de/src/api/v2/resource/resources-endpoint.spec.ts#L243-L244