AlexDenisov/iActiveRecord — pull request #28 by mgod (2013-04-11 15:21:40+00:00)
Title: Fix NSDecimalNumber precision on saving to sqlite

We're using NSDecimalNumber to save currency values, and the current conversion often adds trailing imprecisions in the numbers on save. For example:

NSDecimalNumber *test = [NSDecimalNumber decimalNumberWithMantissa:1123563 exponent:-3 isNegative:NO];
NSLog(@"number converts: %@", [test stringValue]);
NSLog(@"number converts: %f", [test floatValue]);

Output:

2013-04-11 10:03:46.934 Splitwise[5028:c07] number converts: 1123.563
2013-04-11 10:03:46.934 Splitwise[5028:c07] number converts: 1123.562988

Using stringValue on NSDecimalNumber is stable for all locales I've tested on (it uses descriptionWithLocale: internally) and converts to something sqlite can deal with. I went ahead and forced en_US_POSIX in the patch, even though I've yet to see an issue with different locales.

The downside of this approach is that there are values that cannot be stored in sqlite that can be created with stringValue. Currently, I believe these will silently translate into incorrect float values, but this might cause crashes now instead.

WARNING: I've been unable to get the unit tests to run. I'd love to get these working, but I can't figure out which target is supposed to run tests or how that has been set up. My test builds, but I haven't been able to confirm that it exposes the problem before the fix.