Skip to content

Instantly share code, notes, and snippets.

@mariano-aguero
Created November 26, 2018 13:18
Show Gist options
  • Save mariano-aguero/eaa6a3f1f96c72db90e3c89e3327b8b0 to your computer and use it in GitHub Desktop.
Save mariano-aguero/eaa6a3f1f96c72db90e3c89e3327b8b0 to your computer and use it in GitHub Desktop.
toBigNumber
/**
* Converts the value passed to a BigNumber instance
* @param {*} value - A number representation
* @param {boolean} [force=true] - If set to false will return 'undefined' when value is not a number or a string
* representation of a number.
* @returns {BigNumber|undefined}
*/
export const toBigNumber = (value, force = true) => {
BigNumber.set({ DECIMAL_PLACES: 18 })
if (isNaN(value) || value === '' || value === null) {
if (force) {
return new BigNumber(0)
} else {
return undefined
}
} else {
return new BigNumber(value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment