-
Notifications
You must be signed in to change notification settings - Fork 742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fromFormat as the opposite to toFormat #286
Comments
Does this work for you? function removeFormatting ({ fromNumberString, withDecimalPoint = '.' }) {
const rx = new RegExp(`[^${withDecimalPoint}\\d]*`, 'g')
return fromNumberString.replace(rx, '')
}
removeFormatting({ fromNumberString: '123 456 789.12345 6789' })
// 123456789.123456789 It works by creating a regular-expression that removes everything that isn't a digit or a decimal-point. |
I don't want to enhance the BigNumber constructor so it can handle // Assumes '-' is only used as a minus sign.
BigNumber.fromFormat = (stringFromToFormat, formatObject) => {
const sep = formatObject.decimalSeparator;
const str = stringFromToFormat.replace(new RegExp(`[^${sep || '.'}\\d-]+`, 'g'), '');
return new BigNumber(sep ? str.replace(sep, '.') : str);
}; (If this function was internal to the library then obviously it would have access to the global |
I agree that the constructor doesn't need to be changed. But a |
probably should be called fromFormat -> parse |
I can't find an easy way to parse a string that was created using
toFormat
. I'd like to see afromFormat
function that does that (based on current configuration) and additionally an option to configure BigNumber to automatically parse the strings passed to the constructor. Something like this:BigNumber.config({ AUTO_PARSE: true });
Does this exist yet? In my case this is only about ignoring the
groupSeparator
and readingdecimalSeparator
as '.'. But it would also have to handle everything else inBigNumber.Format
.The text was updated successfully, but these errors were encountered: