Created
November 26, 2018 13:18
-
-
Save mariano-aguero/eaa6a3f1f96c72db90e3c89e3327b8b0 to your computer and use it in GitHub Desktop.
toBigNumber
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
/** | |
* 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