davedelong / DDMathParser

String → Number
MIT License
854 stars 153 forks source link

Support localized decimal numbers with no leading zero #157

Closed brutella closed 4 years ago

brutella commented 6 years ago

With these changes LocalizedNumberExtractor supports decimal numbers with no leading zeros, e.g. ",2" for locale de_AT will be 0.2.

The one thing that's tricky is to bypass the check if the string is a valid number string. LocalizedNumberExtractor.canParseString only returns true, if the NumberFormatter can parse the string – "," cannot be parsed.

What I do instead is to check if the string is a valid prefix string, which means it only contains the decimal separator. I also have to check for start == 0 because of cases where , is used as the argument separator in functions. Because of this workaround I'm unable to parse "-,2" as -0.2.

@davedelong Do you know a better way to do this?

davedelong commented 4 years ago

I rebased your branch off master so it'd have a cleaner merge and uses the latest APIs. Please check out #170