dasch-swiss / dsp-js-lib

JavaScript library that handles API requests to DSP / Knora API
https://dasch-swiss.github.io/dsp-js-lib/
GNU Affero General Public License v3.0
6 stars 3 forks source link

Handle Precision in Decimal Numbers #126

Open tobiasschweizer opened 4 years ago

tobiasschweizer commented 4 years ago

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

musicEnfanthen commented 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);
    } 
tobiasschweizer commented 4 years ago

@musicEnfanthen I will look at that, thanks!