Skip to content

Instantly share code, notes, and snippets.

@vtno
Last active August 22, 2017 05:13
Show Gist options
  • Save vtno/af75d32db0487b72e64f2916ff8042df to your computer and use it in GitHub Desktop.
Save vtno/af75d32db0487b72e64f2916ff8042df to your computer and use it in GitHub Desktop.
handle floating point annoying operation
const preciseOperation = f => (a, b, decimalDigits) => {
decimalDigits = decimalDigits || 12
+(f(a, b)).toFixed(decimalDigits)
}
const add = (a, b) => a + b
const minus = (a, b) => a - b
const multiply = (a, b) => a * b
const divide = (a, b) => a / b
export const preciseAdd = (a, b, decimalDigits) => preciseOperation(add)(a,b, decimalDigits)
export const preciseMinus = (a, b, decimalDigits) => preciseOperation(minus)(a,b, decimalDigits)
export const preciseMultiply = (a, b, decimalDigits) => preciseOperation(multiply)(a,b, decimalDigits)
export const preciseDivide = (a, b, decimalDigits) => preciseOperation(divide)(a,b, decimalDigits)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment