Last active
April 13, 2022 18:02
-
-
Save lyatziv/ead4d99a7b99a6824bb354bc137f30d3 to your computer and use it in GitHub Desktop.
javascript math is weird.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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