Skip to content

Commit

Permalink
Fix precision
Browse files Browse the repository at this point in the history
- Handle small precision properly
- Zero precision will return value rounded to integer
  • Loading branch information
facelessuser committed Feb 10, 2024
1 parent b896c0d commit 9370461
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@ export function skipNone (n) {
* @param {number} precision - Number of significant digits
*/
export function toPrecision (n, precision) {
n = +n;
if (n === 0) {
return 0;
}
precision = +precision;
const multiplier = Math.pow(10, precision - Math.floor(Math.log10(Math.abs(n))) - 1);
return Math.round(n * multiplier) / multiplier;
let integer = ~~n;
let digits = 0;
if (integer && precision) {
digits = ~~Math.log10(Math.abs(integer)) + 1;
}
const multiplier = 10.0 ** (precision - digits);
return Math.floor(n * multiplier + 0.5) / multiplier;
}

const angleFactor = {
Expand Down

0 comments on commit 9370461

Please sign in to comment.