Skip to content

Instantly share code, notes, and snippets.

@lyatziv
Last active April 13, 2022 18:02
Show Gist options
  • Save lyatziv/ead4d99a7b99a6824bb354bc137f30d3 to your computer and use it in GitHub Desktop.
Save lyatziv/ead4d99a7b99a6824bb354bc137f30d3 to your computer and use it in GitHub Desktop.
javascript math is weird.
export const addFixed = (a: number, b: number, p = 0) => {
const countDecimal = (a: number): number => String(a).split('.')[1].length;
const dec = isNaN(p) ? Math.max(countDecimal(a), countDecimal(b)) : p;
const t = Number(`1${'0'.repeat(dec)}`);
return (a * t + b * t) / t;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment