2

I can't make heads or tails of this. I am using NSDecimalNumber to truncate the fractional portion from a string. This works in most cases, but not apparently in the case of infinite decimals (or just too many). Here is an example:

print(NSDecimalNumber(string: "49.81666666666666666").longLongValue)
print(NSDecimalNumber(string: "49.816666666666666666").longLongValue)
print(NSDecimalNumber(string: "49.8166666666666666666").longLongValue)

The first line prints 49, the second -5, and the last one 0. I know I can use the rounding function to do the same thing, and that is what I will probably use instead, but doesn't this seem odd? I know it isn't just converting the float bit pattern into a long or else the results would be completely different.

rickster
  • 124,678
  • 26
  • 272
  • 326
absmiths
  • 1,144
  • 1
  • 12
  • 21
  • 1
    I'd file a bug with Apple on this. I just experimented with the same strings using Objective-C and saw lots of strange results (on a 5th gen iPod touch with iOS 9.2). `intValue` worked but `integerValue` didn't. `longValue` and `longLongValue` didn't work. `doubleValue` worked. – rmaddy Dec 18 '15 at 16:38
  • Go ahead and [file that bug](http://bugreport.apple.com), but if your main objective is converting strings to numbers, you'd probably do better to use [`NSNumberFormatter`](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumberFormatter_Class/index.html) anyway. – rickster Dec 18 '15 at 18:46

0 Answers0