ethers-io / ethers.objc

Fast, simple and complete library for Ethereum in Objective-C.
MIT License
157 stars 57 forks source link

Add support for comma as decimal delimiter #3

Closed beatrupp closed 7 years ago

beatrupp commented 7 years ago

This adds support for "," as decimal delimiter.

The implementation is rather quirky based on the current structure of the string formatting. I added new unit tests, everything should be backwards compatible.

Part 1 to solve https://github.com/ethers-io/EthersWallet-ios/issues/8

ricmoo commented 7 years ago

Heya, thanks!

I need to think a bit more about this solution. Because of the distributed nature of web apps, the locale of the system providing the value to be parsed may not be the same as the locale of the current system.

For the mentioned issue in EtehrsWallet-ios, it makes sense for the UI to perform that transformation, since it knows the input is coming from its keyboard.

I need to look through to make sure I'm not doing automatic comma squelching, but if not I will likely add delimiter agnostic behaviour (so, regardless 1.234 and 1,234 both get parsed equally). If I am, I will likely remove this as I don't think it is unfair to have the library user have to sanitize data to the current locale's bare format.

This is also the reason I enforce that the format will always contain a whole, decimal, fraction. So, you are guaranteed 0 is "0.0". This makes doing additional formatting easier, since you know there will always be exactly 1 decimal place, regardless of the value.

The one concern is the US locale which uses both "." and ","; in en_us 1,234 means "1234.0" whereas in fr_ca 1,234 means "1.234".

ricmoo commented 7 years ago

This has been added to the UI side. The library side will continue using decimal places so it remains locale agnostic. The code for the updated EthersWallet-ios will be posted soon.