Skip to content
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

Open
claudemartin opened this issue Mar 4, 2021 · 4 comments
Open

fromFormat as the opposite to toFormat #286

claudemartin opened this issue Mar 4, 2021 · 4 comments

Comments

@claudemartin
Copy link

I can't find an easy way to parse a string that was created using toFormat. I'd like to see a fromFormat 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 reading decimalSeparator as '.'. But it would also have to handle everything else in BigNumber.Format.

@shuckster
Copy link

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.

@MikeMcl
Copy link
Owner

MikeMcl commented May 9, 2021

I don't want to enhance the BigNumber constructor so it can handle toFormat strings, but following shuckster a fromFormat function could be added using, for example

// 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 FORMAT object and so it wouldn't need to be passed in.)

@claudemartin
Copy link
Author

I agree that the constructor doesn't need to be changed. But a fromFormat like this would be nice. Note it should also allow e as in BigNumber.fromFormat("1e-3") and everything in ALPHABET. And then there's ±Infinity and NaN. It should be able to handle anything that toFormat could possibly return but also everything the constructor would allow. Pre- and suffix could be removed to prevent problems with them containing digits, especially in combination with a custom alphabet.

@povilass
Copy link

probably should be called fromFormat -> parse
parse('1000.000-e', {format: '', groupSeparator, ...etc})
parse('1000.000-e')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants